Last active
December 17, 2015 20:59
-
-
Save danielgolden/5671133 to your computer and use it in GitHub Desktop.
A polyfill for the input placeholder attribute.
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
/** | |
* placefill.js - A polyfill for the placeholder attribute. | |
* @author Daniel Golden <[email protected]> | |
*/ | |
var $place_holder_inputs = $('input[placeholder]'); | |
var place_holder_text = $('input').attr('placeholder'); | |
// make value attribute of all inputs with placeholder attribute equal to | |
// value of placeholder attribute | |
$($place_holder_inputs).each(function () { | |
$(this).val($(this).attr('placeholder')); | |
}); | |
// when input with placeholder attribute is focused on, if | |
// the value is equal to the placeholder attribute | |
// then make then empty the value of the input | |
$('input[placeholder]').focus(function () { | |
if($(this).val() == $(this).attr('placeholder')) { | |
$(this).val(''); | |
} | |
}); | |
// when input with placeholder attribute is blured, if the | |
// input value is blank then make the input value | |
// equal to the contents of the placeholder attribute | |
$('input[placeholder]').blur(function () { | |
if($(this).val() == "") { | |
$(this).val($(this).attr('placeholder')); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment