So I’m trying to populate a HTML dropdown using Javascript/Jquery. I’d like each <option> tag to have some data associated with it, stuff like searchPurpose, searchTime, startDate, endDate. I then have two questions-
- How should I associate that data – through data tags, or through something else?
- How do I access that data once my user has clicked on a certain option?
Thanks
With HTML5,
data-*attributes are definitely the way to go. Suppose you have the following:To access their value with plain JavaScript use:
With jQuery use
data():DEMO.
DEMO (jQuery).