1

I have a text filed in my form. The color of the text in the text field should be in grey color. If i click it and type, the text should turn black and remains to be in black color.

I used the following css.

input[type=text]{ color:#999999;}
input[type=text]:focus{ color:#000;}

This is my html code

<input name="address" id="address" type="text" onFocus="if(this.value =='Address' ) this.value=''" onBlur="if(this.value=='') this.value='Address'" value="Address">

Now the text turns to black color when i am typing. But it turns back to grey color after typing. I need it to be in black after typing. How can i do that?

2
  • so you mean you dont want to change the color of your text when typing and after typing right? Commented Jul 17, 2012 at 4:46
  • 2
    Why can't you use the placeholder attribute for the input field? Commented Jul 17, 2012 at 4:48

4 Answers 4

2

How about

<input name="address" id="address" type="text" 
       onFocus="if(this.value =='Address' )this.value='';this.style.color='#000';" 
       onBlur="if(this.value=='') this.value='Address'; if (this.value=='Address') this.style.color='#999';" 
       value="Address">
Sign up to request clarification or add additional context in comments.

1 Comment

Hi, one more help. If i click the reset button the input text should turn to grey again. Any suggestion?
0

Not sure your question is 100% clear but why don't you just reverse the values in your CSS?

<style>
input[type=text]{ color:#000;}
input[type=text]:focus{ color:#999999;}
</style>

<p>
<input name="address" id="address" type="text" onFocus="if(this.value =='Address' ) this.value=''" onBlur="if(this.value=='') this.value='Address'" value="Address">
</p>

With this the text is black and on focus it's gray but after focus it goes back to black.

Comments

0

I have used JQuery to solve your problem. Please have a look at this JSFiddle

http://jsfiddle.net/EvnQj/

Comments

0

For Above issue, please write HTML as below:

<input name="address" id="address" type="text" onFocus="if(this.value =='Address' ) this.value=''" onBlur="if(this.value==''){ this.value='Address'; } else{ this.style.color='#000'; }" value="Address">

CSS as it is:

input[type=text]{
  color:#999999;
}
input[type=text]:focus{
  color:#000;
}

and if you want to test your bin then http://codebins.com/bin/4ldqpaq

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.