Search code examples
javascriptattributes

Add attributes to <option> from javascript


I have this piece of JS:

for(var index in response.rates) {
    select.options[select.options.length] = new Option([index], index);
}

It generates the and tags populated with an array.

How can I give attributes to from there? Like onselect and and other things.


Solution

  • Assign the option to a variable then you can give it attributes.

    for(var index in response.rates) {
        let option = new Option(index, index);
        option.addEventListener("select", ...);
        select.options[select.options.length] = option;
    }