Skip to content

Instantly share code, notes, and snippets.

@praveenvijayan
Created September 20, 2012 13:09
Show Gist options
  • Save praveenvijayan/3755797 to your computer and use it in GitHub Desktop.
Save praveenvijayan/3755797 to your computer and use it in GitHub Desktop.
jQuery cross-browser html5 placeholder plugin
$(function(){
"use strict";
if(!document.createElement('input').hasOwnProperty("placeholder")){
var placeholderFocus = function(elem,evt){
var placeholder = $(elem).attr('placeholder'),
val = $(elem).val();
if(evt === 'focus'){
return function(){
val = $(elem).val();
if(placeholder === val){
$(elem).val('');
}
}
}else{
return function(){
if($(elem).val() === ''){
$(elem).val(placeholder);
}
}
}
}
$('input[type="text"], textarea').each(function(i,val){
$(val).val($(val).attr('placeholder'));
$(val).on({
'focus':placeholderFocus(this,"focus"),
'blur':placeholderFocus(this,"blur")
});
});
$('form').submit(function(){
$("[placeholder]", this).each(function(index, elem){
$(elem).val('');
});
});
}
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment