[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1005
  • Last Modified:

onFocus delete text from textbox

Hi,
I have a form like this;

<form name="form1" method="post" action="process_applications.php">

<!-- Lots of form fields here like radio buttons etc-->

<input type="text" name="custom[1]" value="Special reason" size=15>
<input type="text" name="custom[12]" value="Special reason" size=15>
<input type="text" name="custom[4]" value="Special reason" size=15>
<input type="text" name="custom[85]" value="Special reason" size=15>
<input type="text" name="custom[11]" value="Special reason" size=15>
</form>

What I want to do is, on focus if custom[x] that textboxs value should be deleted if the value is not "Special reason"

I used to use something like this before:

<script>
function clearText(thefield){
if (thefield.defaultValue==thefield.value)
thefield.value = ""
}
</script>

but its not working for some reason with the above form even after I tried modifying it....

Thanks,
Mag
0
mag1c1an
Asked:
mag1c1an
  • 6
  • 5
1 Solution
 
knightEknightCommented:
<input type="text" name="custom[11]" value="Special reason" size=15 onfocus='if(this.value!="Special reason")this.value="";' />
0
 
knightEknightCommented:
reversing the quotes for convenience:

<input type="text" name="custom[11]" value="Special reason" size=15 onfocus="if(this.value!='Special reason')this.value='';" />
0
 
mag1c1anAuthor Commented:
Damn...i must have posted this 10 seconds before you responded!!!

Problem is....its not working :-(

I tried it in Firefox and IE6...

Thanks,
Mag
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
knightEknightCommented:
oh I see ... any default value ... ok then:

<input type="text" name="custom[11]" value="Special reason" size=15 onfocus="if(this.value!=this.defaultValue)this.value='';" />
0
 
mag1c1anAuthor Commented:
ok, got it...I just changed the != to == and it works...
0
 
knightEknightCommented:
oh ... ok good!  I must have mis-understood the question.
0
 
mag1c1anAuthor Commented:
Maybe...but you helped me find the solution....full points and my thanks!

:-)

Mag
0
 
mag1c1anAuthor Commented:
I was kind of hopeing for onBlur="functionName()" as its less code and the page would load faster.... (coz upto 50 of these textboxes can be on the page...) if you get time please see if thats possible...either way I have already awarded you the points.

Thanks,
Mag
0
 
knightEknightCommented:
no problem ... stand by
0
 
knightEknightCommented:
function myFunction(field)
{
   if( field.value == field.defaultValue )
         field.value="" ;
}

<input type="text" name="custom[11]" value="Special reason" size="15"  onfocus="myFunction(this);" />

0
 
ZvonkoSystems architectCommented:
Reduce the size by script event handler assignment.
Like this:

<head>
<script>
function clearText(thefield){
  if (thefield.defaultValue!=thefield.value){
    thefield.value = ""
  }
}
function init(){
  var elem = document.form1.elements;
  for(var i=0;i<elem.length;i++){
    if(elem[i].name.indexOf("custom[")==0){
      elem[i].onfocus=function(){clearText(this)}
    }
  }
}
</script>
</head>
<body onLoad="init()">

<form name="form1" method="post" action="process_applications.php">

<!-- Lots of form fields here like radio buttons etc-->

<input type="text" name="custom[1]" value="Special reason" size=15>
<input type="text" name="custom[12]" value="Special reason" size=15>
<input type="text" name="custom[4]" value="Special reason" size=15>
<input type="text" name="custom[85]" value="Special reason" size=15>
<input type="text" name="custom[11]" value="Special reason" size=15>
</form>
</body>

0
 
mag1c1anAuthor Commented:
Hi guys,

Thanks! This is working fine for me:

<script>
 function myFunction(field)
{
   if( field.value == field.defaultValue )
         field.value="" ;
}
</script>


I dont really understand the second part (the one with init() ) and i dont think i can use it coz I dont have control over the <body> tag....this whole thing goes into a php script and the client will be able to pick the header template and the footer template....the <body> tag i guess will be in the header template...

See you guys later.

Mag
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 6
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now