Created
September 20, 2012 13:09
-
-
Save praveenvijayan/3755797 to your computer and use it in GitHub Desktop.
jQuery cross-browser html5 placeholder plugin
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(){ | |
"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