Created
April 8, 2014 11:01
-
-
Save boldfacedesign/10109878 to your computer and use it in GitHub Desktop.
Placeholder Polyfil
This file contains 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
//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