Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.