Created
January 4, 2013 15:48
-
-
Save adrianengine/4453598 to your computer and use it in GitHub Desktop.
A simple snippet to detect placeholder support with jQuery on form inputs and fallback accordingly.
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
//Find if user browser supports placeholder | |
jQuery(function() { | |
jQuery.support.placeholder = false; | |
test = document.createElement('input'); | |
if('placeholder' in test) jQuery.support.placeholder = true; | |
}); | |
if(!$.support.placeholder) { | |
// If placeholder is not supported | |
var inputPlaceholder= function () { | |
var input = $('form').find('input[placeholder]'); | |
input.each(function (index) { | |
$(this).val($(this).attr('placeholder')); | |
}); | |
input.on('focus', function () { | |
if ($(this).val() == $(this).attr('placeholder')) { | |
$(this).val(''); | |
}; | |
}); | |
input.on('blur', function () { | |
if ($(this).val() == '') { | |
$(this).val($(this).attr('placeholder')); | |
}; | |
}); | |
} | |
inputPlaceholder(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment