Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Generate a placeholder attribute from the datalist associated with that input.
(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);
Owner

adactio commented Aug 12, 2012

Here's the thinking behind this little bit of code: http://adactio.com/journal/5663/

How about an online demo?

Owner

adactio commented Aug 12, 2012

@mathiasbynens Go for it.

Owner

adactio commented Aug 13, 2012

@mathiasbynens Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment