I would choose a different approach (this is not quite my idea, but I can not find a source for a loan):
1st - Use the html5 property "placeholder".
2nd - use Modernizr.js to find support for placeholders in the browser and a simple jQuery script to add support for browsers that don't support it.
So html will look something like this:
<input type="text" class="placeholder" placeholder="Help Text" /> <textarea class="placeholder" placeholder="Another help text"></textarea>
css:
.placeholder{color:#ccc;}
And javascript:
/* Set placeholder for browsers that don't support HTML5 <input placeholder='text'> * Depends on Modernizr v1.5 */ if (!Modernizr.input.placeholder){ $('input[placeholder], textarea[placeholder]') .focus(function() { var input = $(this); if (input.val() == input.attr('placeholder')) { input.val(''); input.removeClass('placeholder'); } }) .blur(function() { var input = $(this); if (input.val() == '') { input.addClass('placeholder'); input.val(input.attr('placeholder')); } }) //Run once on load .blur(); // Clear all 'placeholders' on submit $('input[placeholder], textarea[placeholder]').parents('form').submit(function() { $(this).find('[placeholder]').each(function() { var input = $(this); if (input.val() == input.attr('placeholder')) { input.val(''); } }); }); }
Tombigel
source share