Input Placeholder Script


/ Published in: jQuery
Save to your folder(s)

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.


Copy this code and paste it in your HTML
  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


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.