Skip to content

Instantly share code, notes, and snippets.

@vikynandha-zz
Created October 17, 2014 05:45
Show Gist options
  • Save vikynandha-zz/ce14015ec4d8981f1c8c to your computer and use it in GitHub Desktop.
Save vikynandha-zz/ce14015ec4d8981f1c8c to your computer and use it in GitHub Desktop.
Polyfill for placeholder atttribute in input elements
input.placeholder {
color: #999;
}
/* Placeholder fallback for IE */
function isPlaceholderSupported() {
if( jQuery.support.placeholder !== undefined ) {
return jQuery.support.placeholder;
}
var test;
jQuery.support.placeholder = false;
test = document.createElement( 'input' );
if( 'placeholder' in test ) { jQuery.support.placeholder = true; }
return jQuery.support.placeholder;
}
if ( ! isPlaceholderSupported() ) {
$( 'input[placeholder]' ).focus( function() {
var $this = $( this );
if ( $this.val() === $this.attr( 'placeholder' )) {
$this.val( '' ).removeClass( 'placeholder' );
}
} ).blur( function() {
var $this = $( this );
if ( $this.val() === '' || $this.val() === $this.attr( 'placeholder' )) {
$this.val( $this.attr( 'placeholder' ) ).addClass( 'placeholder' );
}
} ).blur()
.closest( 'form' ).on( 'submit', function() {
var $inp = $( this ).find( 'input.placeholder' );
$inp.each( function( i, $elem ) {
$elem = $( $elem );
if( $elem.val() === $elem.attr( 'placeholder' )) {
$elem.val( '' ).removeClass( 'placeholder' );
}
} );
} );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment