Click to See Complete Forum and Search --> : Clear Text Field Function onfocus


jhorn17
11-22-2002, 02:14 PM
I have a function that clears the text field when the user selects that text field. I would like to do the same kind of thing for the style of the text field.

here is my code:

function clearText(thefield){
if (thefield.defaultValue==thefield.value)
thefield.value = ""
}

<input type="text" name="attorney_name" value="enter attorney name here" style="color:#666;" onfocus="clearText(this);clearStyle(this);" />

For example, when the page loads, the "enter attorney name here" text will be gray, then the text field is cleared. When the user enters new text into the field, I would like the color of that text to be black.

Is there a way to create a function to change the "style" like we did for the "value"?

Thanks,
Jon

gil davis
11-22-2002, 03:36 PM
This is untried, but it ought to work in IE 4+ and NS 6+:

theField.style.color = "black";

jhorn17
11-22-2002, 03:42 PM
works perfectly!
thanks gil!

Ckore
08-08-2006, 11:56 PM
Been looking for awhile and found this, Got in working in a few seconds.
Thanks,
R

I have a function that clears the text field when the user selects that text field. I would like to do the same kind of thing for the style of the text field.

here is my code:

function clearText(thefield){
if (thefield.defaultValue==thefield.value)
thefield.value = ""
}

<input type="text" name="attorney_name" value="enter attorney name here" style="color:#666;" onfocus="clearText(this);clearStyle(this);" />

For example, when the page loads, the "enter attorney name here" text will be gray, then the text field is cleared. When the user enters new text into the field, I would like the color of that text to be black.

Is there a way to create a function to change the "style" like we did for the "value"?

Thanks,
Jon