Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery $.removeAttr("selected") changes selected option

Please take a look at the example: http://jsfiddle.net/HHDpK/1/

As you see the difference between two choosers is only the line

$("#chooser-1 .y").removeAttr("selected");

But as a result their states are different (especially in Chrome). Am I missing anything, or it is a bug?

like image 455
Maxim Avatar asked Nov 22 '11 12:11

Maxim


2 Answers

Looks like internal bug of jQuery with attributes.

Note that in older versions, before .prop() was introduced in 1.6 version, it works as expected.

In the newer versions, just use .prop() to deal with such properties of elements:

$("#chooser-1 .x").prop("selected", "selected");
$("#chooser-1 .y").removeProp("selected");

jsFiddle update.

More than that - using .removeProp("selected") on the element selected previously with prop() will cause the original selection to return instead of having nothing selected - ideal behavior.

like image 154
Shadow Wizard Hates Omicron Avatar answered Oct 20 '22 19:10

Shadow Wizard Hates Omicron


I found the above answer failed as well, I had to set the prop to false to get the selected items to not be.

$("#chooser-1 .x").prop("selected", false);

removeProp cause undesired effects such as not being able to re-select the options again.

I use jquery 1.6.2

like image 3
Will Bowman Avatar answered Oct 20 '22 20:10

Will Bowman