Create a gist now

Instantly share code, notes, and snippets.

Embed
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);
@adactio

This comment has been minimized.

Show comment
Hide comment
@adactio

adactio Aug 12, 2012

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

Owner

adactio commented Aug 12, 2012

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

@mathiasbynens

This comment has been minimized.

Show comment
Hide comment
@mathiasbynens

mathiasbynens Aug 12, 2012

How about an online demo?

How about an online demo?

@adactio

This comment has been minimized.

Show comment
Hide comment
Owner

adactio commented Aug 12, 2012

@mathiasbynens Go for it.

@mathiasbynens

This comment has been minimized.

Show comment
Hide comment
@adactio

This comment has been minimized.

Show comment
Hide comment
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