Skip to content

Instantly share code, notes, and snippets.

@adactio
Created August 12, 2012 15:54
  • Star 17 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save adactio/3332444 to your computer and use it in GitHub Desktop.
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
Copy link
Author

adactio commented Aug 12, 2012

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

@mathiasbynens
Copy link

How about an online demo?

@adactio
Copy link
Author

adactio commented Aug 12, 2012

@mathiasbynens Go for it.

@mathiasbynens
Copy link

@adactio
Copy link
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