admin管理员组文章数量:1424429
I'm absolutely new to JavaScript and would like to modify a textarea of a form (which is generated by an external script) as follows:
1.) Textarea on start: Labeled 'Your message here' in color 'rgb(136, 136, 136)'
2.) Textarea on focus: Label removed and color set to 'rgb(0, 0, 0)'
3.) Textarea on blur: Color of user input set to 'rgb(136, 136, 136)'; if there's no input, label reappears in color 'rgb(136, 136, 136)'
I've experimented around with
var foo = document.getElementById('HCB_textarea');
foo.innerHTML = 'Your message here';
foo.style.color = 'rgb(136, 136, 136)';
foo.onfocus = do something;
foo.onblur = do something;
but didn't get it right. TIA
I'm absolutely new to JavaScript and would like to modify a textarea of a form (which is generated by an external script) as follows:
1.) Textarea on start: Labeled 'Your message here' in color 'rgb(136, 136, 136)'
2.) Textarea on focus: Label removed and color set to 'rgb(0, 0, 0)'
3.) Textarea on blur: Color of user input set to 'rgb(136, 136, 136)'; if there's no input, label reappears in color 'rgb(136, 136, 136)'
I've experimented around with
var foo = document.getElementById('HCB_textarea');
foo.innerHTML = 'Your message here';
foo.style.color = 'rgb(136, 136, 136)';
foo.onfocus = do something;
foo.onblur = do something;
but didn't get it right. TIA
Share Improve this question asked Apr 13, 2010 at 14:35 petepete 331 silver badge3 bronze badges2 Answers
Reset to default 2Presuming by 'label' you don't mean a label HTML element, but instead default text in the textrea as your example seems to suggest, try this:
var foo = document.getElementById('HCB_textarea');
var defaultText = 'Your message here';
foo.value = defaultText;
foo.style.color = '#888';
foo.onfocus = function(){
foo.style.color = '#000';
if ( foo.value == defaultText ) {
foo.value = '';
}
};
foo.onblur = function(){
foo.style.color = '#888';
if ( foo.value == '' ) {
foo.value = defaultText;
}
};
That looks pretty close to me. You have to color all or none of the textarea, you can't color certain characters without some crazy hacks.
var foo = document.getElementById('HCB_textarea');
var labelVal = 'Your message here'
foo.value = origVal;
foo.style.color = 'rgb(136, 136, 136)';
foo.onfocus = function() {
if( foo.value == labelVal ) foo.value = "";
foo.style.color = 'rgb(136, 136, 136)';
}
foo.onblur = function() {
if( foo.value != "" ) {
foo.style.color = 'rgb(0, 0, 0)';
} else {
foo.value = labelVal;
foo.style.color = 'rgb(136, 136, 136)';
}
}
// You should modify this to use your actual form name.
document.forms[0].onsubmit = function() {
if( foo.value == labelVal ) foo.value = "";
}
EDIT - I modified the code because Mario pointed out that you meant you wanted the label to be inside of the textarea, not a <label>
element.
本文标签: Styling a textarea with JavaScriptStack Overflow
版权声明:本文标题:Styling a textarea with JavaScript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745415277a2657655.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论