javascript - How can I change the colour of a textarea's text depending on whether it is the placeholder or not? - Stack

I basically want to recreate the HTML5 "placeholder" attribute, using JavaScript, so that it

I basically want to recreate the HTML5 "placeholder" attribute, using JavaScript, so that it is patible with older browsers.

I am using a bination of OnFocus and OnBlur which is easy enough, I have done that with the following code;

<textarea onfocus="if(this.value==this.defaultValue)this.value='';" onblur="if(this.value=='')this.value=this.defaultValue;">Placeholder text</textarea>

The placeholder text is going to be a light grey (#CCC) and then when the textarea is focused on it will change to a darker grey (#333). This is acplished without a problem in the CSS, however, if the content of the textarea is different to the original placeholder, I want the colour to remain the dark grey when not focused.

If my explanation is poor, then here is a (very slightly) better explanation;

Page loads ---> textarea placeholder = #CCC ---> user focuses on textarea ---> textarea content bees #333 ---> user releases focus ---> the content should change back to #CCC unless it has been altered, in which case it should stay #333

The bold bit is what I need help with!

Thanks! :D

Edit: The fact that the placeholder disappears on focus is not an issue and doesn't need to be "fixed"

I basically want to recreate the HTML5 "placeholder" attribute, using JavaScript, so that it is patible with older browsers.

I am using a bination of OnFocus and OnBlur which is easy enough, I have done that with the following code;

<textarea onfocus="if(this.value==this.defaultValue)this.value='';" onblur="if(this.value=='')this.value=this.defaultValue;">Placeholder text</textarea>

The placeholder text is going to be a light grey (#CCC) and then when the textarea is focused on it will change to a darker grey (#333). This is acplished without a problem in the CSS, however, if the content of the textarea is different to the original placeholder, I want the colour to remain the dark grey when not focused.

If my explanation is poor, then here is a (very slightly) better explanation;

Page loads ---> textarea placeholder = #CCC ---> user focuses on textarea ---> textarea content bees #333 ---> user releases focus ---> the content should change back to #CCC unless it has been altered, in which case it should stay #333

The bold bit is what I need help with!

Thanks! :D

Edit: The fact that the placeholder disappears on focus is not an issue and doesn't need to be "fixed"

Share Improve this question asked Aug 25, 2012 at 11:20 Michael RutaMichael Ruta 311 silver badge2 bronze badges 1
  • Just to clear things up, if the text is different to "Placeholder text" then the colour should remain as #333 when the textarea loses focus. If the text is "Placeholder text" or left blank, then the colour should revert to #CCC. – Michael Ruta Commented Aug 25, 2012 at 11:38
Add a ment  | 

2 Answers 2

Reset to default 3

http://jsfiddle/DszSR/

​textarea {
  color: #ccc;
}​

<textarea
  onfocus="if(this.value==this.defaultValue) this.value=''; this.style.color = '#333';"
  onblur="if(this.value=='') this.value=this.defaultValue; this.style.color = '#ccc';">Placeholder text</textarea>​

You still have to set the text-color to #CCC on page load with CSS or JS (My code overrides that setting if it gets focused and restores it on blur only if needed)

<textarea onfocus="if(this.value==this.defaultValue)this.value='';this.style.color='#333'" onblur="if(this.value=='') {this.value=this.defaultValue;this.style.color='#CCC'}">Placeholder text</textarea>​

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745260948a4619210.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信