Opened 11 years ago
Closed 11 years ago
#11535 closed bug (wontfix)
Inconsistent behavior between .val() and .prop("value") for <select>
Reported by: | kbondar | Owned by: | Timmy Willison |
---|---|---|---|
Priority: | low | Milestone: | 1.next |
Component: | attributes | Version: | 1.7.2 |
Keywords: | Cc: | ||
Blocked by: | Blocking: |
Description
When <select> (select-one) has <options> with duplicate values, setting the value .prop("value", "value") selects first option, while using .val("value") selects last option.
The problem lies in jQuery.valHooks.select.set
It might be worth to bail out of .each() as soon as option is selected and select type is select-one - also for performance reasons
Change History (11)
comment:1 follow-up: 4 Changed 11 years ago by
Milestone: | None → 1.8 |
---|---|
Status: | new → open |
comment:2 Changed 11 years ago by
Component: | unfiled → attributes |
---|---|
Priority: | undecided → low |
comment:3 follow-up: 5 Changed 11 years ago by
The val code for selects is shared between multiple and select-one types. This is great for us in terms of maintainability and size without a loss in performance. So, if we can keep from forking the code path, that would be awesome. As far as prop is concerned, it is a very bare method that should stay that way. I'd say use at your own risk and know that you are working with raw properties.
comment:4 Changed 11 years ago by
Seems to work even in IE6
Specified in HTML DOM Level 1, though I believe early Safari versions did not supported it - select.value was added for sure in Safari 4.
FF and Opera have it for ages.
comment:5 Changed 11 years ago by
Replying to timmywil:
The val code for selects is shared
The problem arose when porting plain JS code to jQuery. If assignment to select.value selects first matching option I believe .val(value) should behave the same. Speaking of performance, getting the value from select.value or even using select.options[select.selectedIndex].value is all the way faster than looping through the list of options, is not it?
comment:6 Changed 11 years ago by
Resolution: | → duplicate |
---|---|
Status: | open → closed |
comment:8 Changed 11 years ago by
Resolution: | duplicate |
---|---|
Status: | closed → reopened |
comment:9 Changed 11 years ago by
Milestone: | 1.8 → 1.next |
---|---|
Owner: | set to Timmy Willison |
Status: | reopened → assigned |
I'll take this, but I'm not too worried about it for 1.8. The solution will probably add some bytes, and it can wait because although the behavior differs from native JS, it will change the way jQuery has always handled this.
comment:10 Changed 11 years ago by
Thinking about it some more, I am not convinced we should do anything. This is an edge case; if someone wants our "official" word on the value with the various valHooks shims they should be using .val()
rather than .prop()
or direct property access.
Kind of a corner case but worth discussion. Seems like it could be fixed in the
.each()
with something likereturn !selectOne || !this.selected;
to bail out early.Which situation and/or browser(s) prevent us from just assigning to the
.value
property on a select-one element? Seems to work even in IE6. I see #2252 but that's not really related.