Created
October 17, 2014 05:45
-
-
Save vikynandha-zz/ce14015ec4d8981f1c8c to your computer and use it in GitHub Desktop.
Polyfill for placeholder atttribute in input elements
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
input.placeholder { | |
color: #999; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* 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