Skip to content

Instantly share code, notes, and snippets.

@gustavopaes
Created November 24, 2015 18:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gustavopaes/55910bdac14c5824e334 to your computer and use it in GitHub Desktop.
Save gustavopaes/55910bdac14c5824e334 to your computer and use it in GitHub Desktop.
simple placeholder polyfill
;(function() {
var testInput = document.createElement('input');
if('placeholder' in testInput || 'placeHolder' in testInput) {
return false;
}
var inputs = document.querySelectorAll('input');
for(var i = 0, m = inputs.length; i < m; i++) {
var input = inputs[i];
var inputPlaceholder = input.getAttribute('placeholder');
if(input.type == 'text' && inputPlaceholder !== '') {
input.value = inputPlaceholder;
input.onclick = function() {
if(input.value == inputPlaceholder) {
input.value = '';
}
}
input.onblur = function() {
if(input.value == '') {
input.value = inputPlaceholder;
}
}
}
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment