Skip to content

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.

Copy link
Owner Author

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.

Copy link

mathiasbynens commented Aug 12, 2012

How about an online demo?

@adactio

This comment has been minimized.

Copy link
Owner Author

adactio commented Aug 12, 2012

@mathiasbynens Go for it.

@mathiasbynens

This comment has been minimized.

Copy link

mathiasbynens commented Aug 13, 2012

@adactio

This comment has been minimized.

Copy link
Owner Author

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
You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.