Posted By

scheurbert on 07/14/10


Tagged

default input text placeholder html5


Versions (?)

Input Placeholder Script


 / Published in: jQuery
 

Just a quick script to enable default text within a form element to be cleared or restored upon focus/blur of a particular element.

Should give you roughly the same functionality as the new HTML5 webforms "placeholder" attribute.

  1. // Placeholder text
  2. jQuery('input[type=text]').bind('focus', function() {
  3. if ( $(this).attr('placeholder') && $(this).val() == $(this).attr('placeholder') ) $(this).val('');
  4. }).bind('blur', function() {
  5. if ( $(this).attr('placeholder') && !$(this).val() ) $(this).val( $(this).attr('placeholder') );
  6. }).each( function() {
  7. if ( !$(this).attr('placeholder') && $(this).val() != "" ) $(this).attr('placeholder', $(this).val() );
  8. });

Report this snippet  

You need to login to post a comment.