/    Sign up×
Community /Pin to ProfileBookmark

Clear Text Field Function onfocus

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

to post a comment
JavaScript

3 Comments(s)

Copy linkTweet thisAlerts:
@gil_davisNov 22.2002 — This is untried, but it ought to work in IE 4+ and NS 6+:

theField.style.color = "black";
Copy linkTweet thisAlerts:
@jhorn17authorNov 22.2002 — works perfectly!

thanks gil!
Copy linkTweet thisAlerts:
@CkoreAug 09.2006 — 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[/QUOTE]
×

Success!

Help @jhorn17 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.24,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...