Link to home
Start Free TrialLog in
Avatar of mag1c1an
mag1c1an

asked on

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
Avatar of knightEknight
knightEknight
Flag of United States of America image

<input type="text" name="custom[11]" value="Special reason" size=15 onfocus='if(this.value!="Special reason")this.value="";' />
reversing the quotes for convenience:

<input type="text" name="custom[11]" value="Special reason" size=15 onfocus="if(this.value!='Special reason')this.value='';" />
Avatar of mag1c1an
mag1c1an

ASKER

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
ASKER CERTIFIED SOLUTION
Avatar of knightEknight
knightEknight
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ok, got it...I just changed the != to == and it works...
oh ... ok good!  I must have mis-understood the question.
Maybe...but you helped me find the solution....full points and my thanks!

:-)

Mag
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
no problem ... stand by
function myFunction(field)
{
   if( field.value == field.defaultValue )
         field.value="" ;
}

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

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>

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