Created
January 27, 2011 04:11
-
-
Save sp3c73r2038/798053 to your computer and use it in GitHub Desktop.
for browsers not supporting HTML5 placeholder attribute
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
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