9

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?

2 Answers 2

7

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.

1
  • 1
    Although it will work but prop() is not set selected attribute in html option element so I'm using both of them like this { .removeProp('selected').removeAttr('selected') } and { .prop('selected', 'selected').attr('selected', 'selected') }
    – QMaster
    Aug 21, 2016 at 14:49
3

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.