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
LVL 2
mag1c1anAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.