admin管理员组文章数量:1417426
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
2 Answers
Reset to default 3http://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>
本文标签:
版权声明:本文标题:javascript - How can I change the colour of a textarea's text depending on whether it is the placeholder or not? - Stack 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745260948a2650355.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论