Skip to content

Instantly share code, notes, and snippets.

@lewisou
Created January 18, 2012 04:30
Show Gist options
  • Save lewisou/1630982 to your computer and use it in GitHub Desktop.
Save lewisou/1630982 to your computer and use it in GitHub Desktop.
Deal with placeholder without browser support
function elementSupportsAttribute(element, attribute) {
var test = document.createElement(element);
if (attribute in test) {
return true;
} else {
return false;
}
};
$(document).ready(function(){
if (!elementSupportsAttribute('input', 'placeholder')) {
$("input[placeholder], textarea[placeholder]").each(function(ind, ele){
$el = $(this);
if($el.val() == '') {
$el.val($el.attr('placeholder') || '');
}
});
$("input[placeholder], textarea[placeholder]")
.focus(function() {
var $el = $(this);
if ($el.val() == $el.attr("placeholder")) {
$el.val('');
}
})
.blur(function() {
var $el = $(this);
if ($el.val() == '') {
$el.val($el.attr("placeholder"));
}
});
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment