views:

159

answers:

2

Hi guys! 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

A: 

Presuming 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;
    }

};
Mario Menger
Avoid setting `innerHTML` on `<textarea>`. It's not supposed to set the value of the field. (In practice, it does, but only sometimes, and unreliably across different browsers.) Use `value`.
bobince
Ah you're right. I was thinking `<label>`.
bradlis7
Hi Mario! This seems to work perfectly except one little thing: The defaultText does not reappear if there has been some user input before which got removed again.
pete
As bobince suggested it would be better to use 'value' instead of 'innerHTML'. So, can I simply replace it ('foo.value = defaultText;')?
pete
Yes, that should be fine, I'll change it in this post too
Mario Menger
Replacing 'innerHTML' by 'value' works like a charm and also solved the issue with the non-reappearing defaultText. Thanks a billion, Mario and bradlis7, you've just made my day!!
pete
+1  A: 

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.

bradlis7
Yes, that's right. I'll try your code...
pete