David Babayan
David Babayan

Reputation: 49

Why element.removeAttribute('selected') not work?

Source code http://jsfiddle.net/51p7ocLw/

I tried to create <select> tag with multyselect but the problem is that in regular you have to push Ctrl to select a few. Now with this function I resolve that problem. But there arise new one. It's not removing selected attribute from element when I'm calling el.removeAttribute('selected')

NOTE: code works on chrome but not works on Edge but I need it to work on both of them!

Upvotes: 0

Views: 1136

Answers (1)

David Hedlund
David Hedlund

Reputation: 129832

This is because the selected state of an option is not an attribute but a property.

Instead of el.removeAttribute('selected'), use el.selected = false.

To toggle, simply use:

el.selected = !el.selected;

Upvotes: 0

Related Questions