2 * HTML5 placeholder emulation for jQuery plugin
4 * This will automatically use the HTML5 placeholder attribute if supported, or emulate this behavior if not.
6 * @author Trevor Parscal <tparscal@wikimedia.org>
11 jQuery.fn.placeholder = function( text ) {
12 // If the HTML5 placeholder attribute is supported, use it
13 if ( 'placeholder' in document.createElement( 'input' ) ) {
14 jQuery(this).attr( 'placeholder', text );
16 // Otherwise, use a combination of blur and focus event handlers and a placeholder class
18 jQuery(this).each( function() {
19 var $input = jQuery(this);
21 // Show on blur if empty
22 .bind( 'blur', function() {
23 if ( $input.val().length == 0 ) {
26 .addClass( 'placeholder' );
30 .bind( 'focus', function() {
31 if ( $input.hasClass( 'placeholder' ) ) {
34 .removeClass( 'placeholder' );
37 // Blank on submit -- prevents submitting with unintended value
39 .bind( 'submit', function() {
40 $input.trigger( 'focus' );
42 // Show initially, if empty
43 if ( $input.val() == '' ) {
44 $input.trigger( 'blur' );