dimanche 3 mai 2015

Using setAttribute on a selected option

I am new to Javascript and I cannot figure out how to use setAttribute on a selected option.

I have a select element in my html with id = employee (adding options using javascript)

<select name="employee" id="employee"></select>

Now, using javascript (or jquery) I am trying to add a "dept" attribute to the selected option dynamically.

However, below code doesn't work. What I am doing wrong here?

var sel = document.getElementById("employee");
var selected = sel.options[sel.selectedIndex];
selected.setAttribute("data-dept", Support);

I also tried:

$("#employee option:selected").setAttribute("data-dept", "Support");  

That also didn't work. Can you help me please?

Aucun commentaire:

Enregistrer un commentaire