Created
March 28, 2012 10:22
-
-
Save vinaydotblog/2225241 to your computer and use it in GitHub Desktop.
Placeholder Support for old browsers using JavaScript
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
// Placeholder Support for old browsers using JavaScript (without jQuery) | |
// Test on IE6+, Firefox, Chrome | |
if( 'placeholder' in document.createElement('input') == false ){ | |
var inputs = document.getElementsByTagName('input'); | |
for(var i = 0 ; i < inputs.length ; i++ ){ | |
var input = inputs[i], placeholder = input.getAttribute('placeholder'), | |
textColor = input.style.color; | |
if(placeholder){ | |
input.value = placeholder; | |
input.style.color = '#aaa'; | |
if ( document.addEventListener ){ | |
input.addEventListener('focus',function(){ | |
if( this.value === this.getAttribute('placeholder') ){ | |
this.value = ''; | |
this.style.color = textColor; | |
} | |
}); | |
input.addEventListener('blur',function(){ | |
if( !this.value ){ | |
this.value = this.getAttribute('placeholder'); | |
this.style.color = '#aaa'; | |
} | |
}); | |
} | |
else{ | |
input.attachEvent('onfocus',function(e){ | |
me = window.event.srcElement; | |
if( me.value === me.getAttribute('placeholder') ){ | |
me.value = ''; | |
me.style.color = textColor; | |
} | |
}); | |
input.attachEvent('onblur',function(){ | |
me = window.event.srcElement; | |
if( !me.value ){ | |
me.value = me.getAttribute('placeholder'); | |
me.style.color = '#aaa'; | |
} | |
}); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment