1

I'm a bit boggled by this... Why is .val() returning null when there clearly is an option selected for the <select>?

These commands were executed via Chrome's console on the page with the issue.

enter image description here

rinogo
  • 8,491
  • 12
  • 61
  • 102
  • 8
    @Horen you shouldn't have to do that, which is kinda the point of this question. – Kevin B Jun 14 '13 at 21:48
  • For what it's worth, it works in every version of jquery available to jsfiddle: http://jsfiddle.net/A2RVE/ – Kevin B Jun 14 '13 at 21:52
  • Weird. I just upgraded to jQuery 1.10.1 - maybe that's causing the problem... – rinogo Jun 14 '13 at 21:53
  • Seems pedantic but it looks like the selector won't give you a value unless you specify that you want the value of the selected option – MrVimes Jun 14 '13 at 21:53
  • It returns the selected option's value (which just so happens to be available directly on the select, try `$('select[name=template]')[0].value`.) – Kevin B Jun 14 '13 at 21:54
  • Well, I want to know the value of the select element, which is indicated by the options... All I know is that this USED to work in all previous versions of jQuery that I've used... :/ – rinogo Jun 14 '13 at 21:54
  • 1
    Maybe jquery just got a bit more strict/pedantic :) – MrVimes Jun 14 '13 at 21:55
  • 1
    Note: Just because one of the options has the `selected` attribute doesn't mean that it actually has to be selected. That's the initial selection, and if the user selects another doesn't mean that the attribute changes. However, as there is no way to select no option unless it's a multiple select, one of the options still has to be selected. – Guffa Jun 14 '13 at 21:55
  • @MrVimes: `select` elements have a `.value` property that will give you the value of the selected element. Even if they didn't it would be easy for jQuery to look at the options and find the selected one. –  Jun 14 '13 at 21:55
  • I just tested with that specific jQuery version, and it worked. – bfavaretto Jun 14 '13 at 21:55
  • 2
    I cannot see any reasonable argument for downvoting. – nothrow Jun 14 '13 at 21:56
  • 1
    I feel like I'm missing something very obvious... Can those who are voting to close this and voting it down please explain why it's a dumb question? :) – rinogo Jun 14 '13 at 21:56
  • People are a lot more downvotey than they used to be on Stack sites. I've had a stackoverflow account for years and only recently do I see lots of questions get downvotes. Edit: I accept that people won't be downvoting for the sake of it, but perhaps a lot less tolerant of small faults with questions than in the past. – MrVimes Jun 14 '13 at 21:57
  • 4
    I think they're voting like that because the issue doesn't seem to be reproducible. In this case, the question cannot be answered in its current form. – bfavaretto Jun 14 '13 at 21:57
  • My guess is you aren't actually using jQuery, but some other framework that just so happens to also have a .val method because the .val method in 1.10 will *never* return null when an element is selected – Kevin B Jun 14 '13 at 21:58
  • 1
    http://stackoverflow.com/questions/4796103/jquery-in-chrome-console-8-0-552-237 – Drahcir Jun 14 '13 at 21:59
  • Thanks, @bfavaretto, I'll try to come up with something more reproducible (and probably find the *real* issue in the meantime!) – rinogo Jun 14 '13 at 21:59
  • What does `$.fn.jquery` give you in the console? – Kevin B Jun 14 '13 at 21:59
  • @rinogo, see Gerve's link in comments. Most likely covers the issue. – nothrow Jun 14 '13 at 22:01
  • @Yossarian and Gerve - Tried it - Doesn't appear to be my issue. jQuery() doesn't work either... Hmm. – rinogo Jun 14 '13 at 22:02
  • @Yossarian I thought that too, but then wouldn't `$.fn.jquery` NOT give `1.10.1`? something strange is going on. – Kevin B Jun 14 '13 at 22:02
  • You're doing the testing in the console at the moment, have you tried testing a simple script in the actual browser? – Drahcir Jun 14 '13 at 22:04
  • @Yossarian: That's referring to a really old version of Chrome. There would be a TypeError in the console when calling `.val()` if that was the case here. –  Jun 14 '13 at 22:04
  • I think @Guffa is right, though it does seem possible to set the select element to no selection in Chrome by assigning `null` to the `.value` of the select. Doesn't work in FF though. http://jsfiddle.net/pzGnV/ –  Jun 14 '13 at 22:07
  • ...to verify, try `$("select[name=template] option:selected").length` –  Jun 14 '13 at 22:09
  • 1
    Agreed, it is definitely possible to reproduce the console screenshot in the question if `val` is being set to `null` somewhere first [as here](http://i.stack.imgur.com/OUvnU.png) – Martin Smith Jun 14 '13 at 22:17

1 Answers1

2

I think this may be a bug in Chrome. Maybe a bug between my specific version of Chrome and this specific version of jQuery? The select in question (which was previously hidden, but unhidden for further testing) appears like so:

enter image description here

This suggests that an option is not selected.

However, clicking on it (to open the select element and display the options, not actually clicking on an option) reveals this:

enter image description here

Weird. Very weird. Soooo if the GUI is reflecting the state of things, part of Chrome thinks that the select element has an option selected, while another part of the code thinks the select element does not have an option selected? Weird.

rinogo
  • 8,491
  • 12
  • 61
  • 102
  • By the way, the select element is added in this case via an indirect method - the culprit of the problem is likely the plugin/code adding the element... – rinogo Jun 14 '13 at 22:12