Created
January 18, 2012 04:30
-
-
Save lewisou/1630982 to your computer and use it in GitHub Desktop.
Deal with placeholder without browser support
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
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