/ Published in: JavaScript
Takes any field with the class .clear-text and makes it clear it's default text (ex: 'search here') when it is focused on. If nothing is changed or the field is left blank, the original text appears when focus leaves the field.
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
Drupal.behaviors.clearText = function(context) { var inputs = $('input.clear-text:not(.processed)'); // Store the original values inputs.each(function() { $this = $(this); $this.data('default', $this.val()); }).addClass('processed'); // Set up inputs to clear on focus & reload default (if blank) on blur inputs .focus(function() { $this = $(this); if ( $this.val() == $this.data('default') ) { $this.val(''); } }) .blur(function() { $this = $(this); if ( $this.val() == '' ) { $this.val( $this.data('default') ); } }); }