Skip to content

Instantly share code, notes, and snippets.

@sp3c73r2038
Created January 27, 2011 04:11
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 sp3c73r2038/798053 to your computer and use it in GitHub Desktop.
Save sp3c73r2038/798053 to your computer and use it in GitHub Desktop.
for browsers not supporting HTML5 placeholder attribute
function activatePlaceholders() {
var detect = navigator.userAgent.toLowerCase();
if (detect.indexOf('safari') > 0) {
return false;
}
var inputs = document.getElementsByTagName('input');
for (var i=0;i<inputs.length;i++) {
if (inputs[i].getAttribute('type') == 'text') {
if (inputs[i].getAttribute('placeholder') && inputs[i].getAttribute('placeholder').length > 0) {
var className = inputs[i].getAttribute('class');
if(inputs[i].value.length < 1){
inputs[i].setAttribute('class', className + " placeholders");
}
inputs[i].onclick = function() {
if (this.value == this.getAttribute('placeholder')) {
var className = inputs[i].getAttribute('class');
className.replace('placeholders', '');
this.setAttribute('class', className);
this.value = '';
}
return false;
}
inputs[i].onblur = function() {
if (this.value.length < 1) {
this.value = this.getAttribute('placeholder');
var className = inputs[i].getAttribute('class');
this.setAttribute('class', className + " placeholders");
}
}
inputs[i].value = inputs[i].getAttribute('placeholder');
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment