-
Notifications
You must be signed in to change notification settings - Fork 20.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Select multiple will return null when no values returned #2562
Comments
It is mentioned in official site of jQuery:
|
I read that page and completely missed that part. Sorry! However, it doesn't explain why this is the case? I don't really understand why you would want null rather than an array since you would expect an array for all other cases (when |
I am not sure why exatly but i guess they told to use like this:
Which is nothing but a short circuit evaluation which is used to set default values! Means you will not have null in that case! Fiddle: http://jsfiddle.net/jzyyhosx/2/ |
If it's documented and it's been that way since jQuery 1.2, we'd most likely be breaking a lot of code by changing its behavior. I don't recall why it's done that way but as @NeelBhatt says it's easy to get the result you want. |
Ugh. We actually have one test that |
@gibson042 I agree, this is too inconsistent to keep it like that. I'll reopen so that others can voice their opinions. |
We should be careful that changing |
We're all set there because |
Can not it be done by putting empty string instead of null here: return val == null ?
"" :
jQuery.isArray( val ) ?
jQuery.map( val, function( val ) {
return { name: elem.name, value: val.replace( rCRLF, "\r\n" ) };
}) :
{ name: elem.name, value: val.replace( rCRLF, "\r\n" ) }; So even though people currently using || [] will not get affected by ORing with empty string. See Demo: http://jsfiddle.net/jzyyhosx/3/ |
That block of code doesn't even need to change because |
This was accepted in today's meeting. |
I forgot what we decided. Do we want to change the |
Both should return an empty array. The idea is that if we ask for a collection (i.e. "select multiple") then we should get a collection, although it might be empty. |
right, thanks. |
We are in the process of migrating to jQuery 3 in one of our projects and ran into this today. We couldn't find this as a breaking change in the upgrade guide nor found the documentation to be exact (emphasis mine).
Did we miss something or is the upgrade guide missing this? |
It's mentioned here: And seems to be working that way in 3.0: There's a docs bug here, it just got missed for the 3.0 api update: |
Thanks! Seems we missed this, we were looking for And is there a way I could help to get this doc update going? :) |
Sure, you can file PRs for jquery/api.jquery.com#828 and one for the the upgrade guide at https://github.com/jquery/jquery.com/, that would be greatly appreciated! |
I've just been looking into an issue whereby if you have a
select
element with themultiple
option set and no options selected$().val()
will returnnull
rather than an empty array. If one more more items are selected, then it returns an array. I would have expected that an empty array would have been returned in this case.This fiddle shows the issue. IT appears all the way back to jQuery 1.2 where multiple value support was added.
The reason an empty array is not returned is the check for
elem.selectedIndex
being < 0 here. While I understand that for a single select, I don't while get why that would be done for a multi-select.Removing
|| index < 0
would allow the empty array to be returned.Having said that, I'm not really expecting this change to be made as I guess a number of sites will depend upon this behaviour. I'm more wondering if someone might be willing to say why it is done this way? I've searched this issue tracker and the bugs.jquery.com site, but couldn't find any mention of it.
The text was updated successfully, but these errors were encountered: