Bug Tracker

Opened 9 years ago

Closed 9 years ago

Last modified 8 years ago

#8512 closed enhancement (patchwelcome)

jQuery("SELECT").val(null) : inconsistent results in different browsers

Reported by: shehriyari Owned by: shehriyari
Priority: low Milestone: 1.next
Component: attributes Version: 1.5.1
Keywords: Cc:
Blocked by: Blocking:

Description (last modified by Rick Waldron)

http://jsfiddle.net/jf7t2/1/

Please run it on the latest versions of all browsers, and see for yourself. When the button is clicked, on:

  1. on WebKit browsers (Chrome, Safari) it just doesn't select anything, instead creates some ghostly empty option
  2. on Firefox and Opera, it works the way I expect and want it to work, de-selects all options of the element
  3. on Explorer, it does nothing

jQuery 1.5.1 source code also tells us that, in this case jQuery sets selectedIndex = -1.

http://stackoverflow.com/questions/5286424/jqueryformelement-valnull-inconsistent-results-in-different-browsers/5286643#5286643

But since browsers mess it up in this case. Shouldn't jQuery force it to behave the way Firefox and Opera does, de-select all child options?

Thanks.

Change History (3)

comment:1 Changed 9 years ago by dmethvin

Component: unfiledattributes
Owner: set to shehriyari
Priority: undecidedlow
Status: newpending
Type: bugenhancement

There is no documented behavior for .val(null), are you asking for that to be an enhancement to select no options then? The W3C has this to say about the case where nothing is selected initially:

http://www.w3.org/TR/html401/interact/forms.html#h-17.6

If no OPTION element has the selected attribute set, user agent behavior for choosing which option is initially selected is undefined. Note. Since existing implementations handle this case differently, the current specification differs from RFC 1866 ([RFC1866] section 8.1.3), which states: The initial state has the first option selected, unless a SELECTED attribute is present on any of the <OPTION> elements. Since user agent behavior differs, authors should ensure that each menu includes a default pre-selected OPTION.

It's not clear whether browsers want you to deselect all options on a select-one element, but the behavior seems to be poorly defined in any case.

comment:2 Changed 9 years ago by shehriyari

Status: pendingnew

I would prefer that, yes, to select no options.

HOWEVER, my wishes are irrelevant: Although it is NOT documented, the CODE is written that way [jQuery source 1.5.1 - check the stackoverflow.com discussion I referred you to, in original report]. What I say, is that you should make a decision, and implement it. Having selectedIndex = -1 doesn't solve problems, it creates them. jQuery's primary objective (I believe) should be "to be standards-compliant AND cross-browser, useful tool that DOES function", not just "to be standards-compliant tool that follows those standards blindly, to no end". selectedIndex = -1 doesn't serve anyone...

And again, YES, I would recommend having all options de-selected. That would solve a lot of problems and would be quite useful shortcut for us, developers.

Thanks for all the effort you guys putting forward, jQuery is an awesome tool! Good luck!

P.S.: That's why I would call this report a BUG, not an ENHANCEMENT, since this little feature doesn't do anything useful for front-end designers.

Last edited 9 years ago by shehriyari (previous) (diff)

comment:3 Changed 9 years ago by Rick Waldron

Description: modified (diff)
Resolution: patchwelcome
Status: newclosed
Note: See TracTickets for help on using tickets.