Created
August 29, 2012 13:33
-
-
Save voronianski/3512573 to your computer and use it in GitHub Desktop.
Placeholder fallback with jQuery
This file contains hidden or 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
/** | |
* Fallback for "Placeholder" attribute in old browsers | |
* source: http://www.scriptiny.com/2012/08/html5-placeholder-fallback-using-jquery/?utm_source=html5weekly&utm_medium=email | |
*/ | |
$(document).ready(function() { | |
if (!('placeholder' in document.createElement('input'))) { | |
$('input[placeholder]').each(function() { | |
var val = $(this).attr('placeholder'); | |
if ( this.value == '' ) { | |
this.value = val; | |
} | |
$(this).focus(function() { | |
if ( this.value == val ) { | |
this.value = ''; | |
} | |
}).blur(function() { | |
if ( $.trim(this.value) == '' ) { | |
this.value = val; | |
} | |
}) | |
}); | |
// Clear default placeholder values on form submit | |
$('form').submit(function() { | |
$(this).find('input[placeholder]').each(function() { | |
if ( this.value == $(this).attr('placeholder') ) { | |
this.value = ''; | |
} | |
}); | |
}); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment