Skip to content

Instantly share code, notes, and snippets.

@vinaydotblog
Created March 28, 2012 10: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 vinaydotblog/2225241 to your computer and use it in GitHub Desktop.
Save vinaydotblog/2225241 to your computer and use it in GitHub Desktop.
Placeholder Support for old browsers using JavaScript
// 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