Skip to content

Instantly share code, notes, and snippets.

@boldfacedesign
Created April 8, 2014 11:01
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 boldfacedesign/10109878 to your computer and use it in GitHub Desktop.
Save boldfacedesign/10109878 to your computer and use it in GitHub Desktop.
Placeholder Polyfil
//POLYFILL PLACEHOLDERS IN IE8/9
if (document.createElement("input").placeholder == undefined) {
//GET ALL TEXT INPUTS
var inp = document.querySelectorAll('input[type=text]');
//LOOP THROUGH AND USE PLACEHOLDER AS VALUE
for (i=0;i<inp.length;i++) {
var plhTxt = inp[i].getAttribute('placeholder');
inp[i].setAttribute('value',plhTxt);
//SET VALUE TO BLANK ON FOCUS IF VALUE = PLACEHOLDER
inp[i].onfocus=function(){
if (this.value === this.getAttribute('placeholder')) {
this.setAttribute('value', '')
}
};
//SET VALUE BACK TO PLACEHOLDER TEXT IF VALUE IS BLANK
inp[i].onblur=function(){
if (this.value == '') {
this.setAttribute('value', this.getAttribute('placeholder'));
}
};
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment