Created
August 12, 2012 15:54
-
-
Save adactio/3332444 to your computer and use it in GitHub Desktop.
Generate a placeholder attribute from the datalist associated with that input.
This file contains hidden or 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(win,doc) { | |
if (doc.querySelectorAll) { | |
var inputs = doc.querySelectorAll('input[list]'), | |
total = inputs.length; | |
for (var i=0; i<total; i++) { | |
var input = inputs[i], | |
id = input.getAttribute('list'), | |
list = doc.getElementById(id), | |
options = list.getElementsByTagName('option'), | |
amount = options.length, | |
rand = Math.floor(Math.random()*amount), | |
option = options[rand], | |
value = option.getAttribute('value'); | |
input.setAttribute('placeholder',value); | |
} | |
} | |
})(this,this.document); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Here's the thinking behind this little bit of code: http://adactio.com/journal/5663/