Skip to content

Instantly share code, notes, and snippets.

@pygeek
Created November 28, 2012 17:08
Show Gist options
  • Save pygeek/4162579 to your computer and use it in GitHub Desktop.
Save pygeek/4162579 to your computer and use it in GitHub Desktop.
<script>
(function($){
//Adding inputFocus to namespace for the placeholder shim
$.fn.inputFocus = function(text){
$(this).focusin(function(){
if($(this).val() == text){
$(this).val('')
}
})
$(this).focusout(function(){
if(!$(this).val().length){
$(this).val(text)
}
})
}
//Placeholder shim
elementSupportsAttribute = function(element,attribute){
//Detects Attribute support for outdated browsers
var test = document.createElement(element);
if (attribute in test){
return true;
}else{
return false;
}
}
if(!elementSupportsAttribute('input','placeholder')){
//Support for outdated browsers
$('input').not(':submit').each(function(i,val){
if($(this).val()=="" && $(this).attr("placeholder")!=""){
var $placeholder = $(this).attr('placeholder');
$(this).val($placeholder)
$(this).inputFocus($placeholder)
}
})
var $textarea_placeholder = $('textarea').attr('placeholder');
$('textarea').val($textarea_placeholder)
$('textarea').inputFocus($textarea_placeholder)
}
})(jQuery);
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment