Closed stelovsky closed 8 years ago
Hi,
Great that someone is responding!
Not true. If no element is selected, .val() returns null, at least in newest Chrome: see below. But can I rely on that in any browser?
And I'd completely disagree that 'idea of a checked or selected element is completely independent of its value' just the opposite, as the purpose of it is 'The .val() method is primarily used to get the values of form elements such as input 'according to the documentation. For $('input[name=radios]:checked').val() we get either the value of the checked radio, or null none is selected. Needless very useful and even stackoverflow doesn't have an entry that says that.
My 2c,
Jan
Thanks,
Jan
Dr. Jan Stelovsky, Founder and CTO
www.parwinr.com, jan AT parwinr.com, +1-808-673-0035 2903 Bunker Hill Ln #150, Santa Clara, CA 95054
This electronic message transmission contains information from the Company parWinr, Inc. that may be proprietary, confidential and/or privileged. The information is intended only for the use of the individual(s) or entity named above. If you are not the intended recipient, be aware that any disclosure, copying or distribution or use of the contents of this information is prohibited. If you have received this electronic transmission in error, please notify the sender immediately by replying to the address listed in the "From:" field.
On Wed, Feb 17, 2016 at 12:18 PM, Dave Methvin notifications@github.com wrote:
Can you clarify the type of information you would like to see there?
The idea of a checked or selected element is completely independent of its value retrieved by .val(), which is why the examples there show how to select a checked element. Is that the concept you think needs to be clarified?
— Reply to this email directly or view it on GitHub https://github.com/jquery/api.jquery.com/issues/893#issuecomment-185432556 .
Let's say you're writing this entry. What text would you add to the documentation to clarify this point?
Dave, let me try:
Also, .val() can be used to find out which - if any - radio button in a group is checked. For instance, $('input[name=radios]:checked').val() returns either null if no radio button in the group radios is checked or the value attribute of the selected radio button.
Hope this helps.
Jan
Thanks,
Jan
Dr. Jan Stelovsky, Founder and CTO
www.parwinr.com, jan AT parwinr.com, +1-808-673-0035 2903 Bunker Hill Ln #150, Santa Clara, CA 95054
This electronic message transmission contains information from the Company parWinr, Inc. that may be proprietary, confidential and/or privileged. The information is intended only for the use of the individual(s) or entity named above. If you are not the intended recipient, be aware that any disclosure, copying or distribution or use of the contents of this information is prohibited. If you have received this electronic transmission in error, please notify the sender immediately by replying to the address listed in the "From:" field.
On Wed, Feb 17, 2016 at 3:36 PM, Dave Methvin notifications@github.com wrote:
Let's say you're writing this entry. What text would you add to the documentation to clarify this point?
— Reply to this email directly or view it on GitHub https://github.com/jquery/api.jquery.com/issues/893#issuecomment-185499092 .
Also, .val() can be used to find out which - if any - radio button in a group is checked. ...
$('input[name=radios]:checked').val()
The finding out is all being done by the selector though so it's not about .val()
. That is, var checks = $('input[name=radios]:checked')
will select all the checked radios, and there may be none. If you wanted to know if any were checked you could just look for checks.length === 0
.
Reading the title again, I think you're asking what $().val()
returns for an empty jQuery selection in general, which applies no matter what you were trying to select. As far as I can tell it's returned undefined
on an empty selection since at least jQuery 1.7, not null
. https://jsfiddle.net/1Lc12g5g/2/
So I see a couple of clarifications we can make here. One, the .val()
method returns the value
property of the first element in the set, other than the special case select-multiple where it's an array of options. Two, if there is nothing in the set then it returns undefined
. Sound good?
Dave,
Sounds great.
I understand completely what $(),val() do. I still maintain that the explanation belongs to val(). Yours is good (the empty set case - as any special return case in entire documentation - should be mentioned; notice my mistake with undefined caused by my reliance on CoffeeScript which tries
In general, is jQuery interested in such comments/suggestions? (Seems so based on your immediate replies.) I'd be happy to offer more, as I use jQuery all the time and would love to save time to other developers.
Frankly, when I face a jQuery problem, I rarely go to jQuery documentation
Also, it's easy to give feedback to MDN (a bit more difficult is it with Google Drive, but still not bad). In contrast, it took me ages to give feedback to jQuery.
Jan
PS. I have MAJOR issues with how jQuery handles parameters in $.when().then() promises. (And I don't mean the 'academic' compliance with other specs, I mean the practical use.) It's too late to change how it is implemented, but at last the documentation should describe the way out of the quagmire: I often want to get several resources and sometimes I don't know how many (may be dependent on previous request. 1) jQuery forces me to use $.when.apply(null, ...) in this case instead of accepting an array. Ugly. 2) Even if I know the promises and pass them as comma-separated parameters, the arguments I get in then() have different structure depending on whether it's one promise or two or more! If it's just one, the result is in arguments[0]. With more promises, the results are in arguments[0][0], arguments[1][0], arguments[2][0], arguments[3][0], etc., i.e., one subarray deeper! This is quite insane and I made a separate module to get just the results. (Why is the extra information in the other array items polluting my requests' results is beyond me... There should be another function I can call to get this info in the unlikely case I should ever need it!). In my module the callback simply has as many arguments as there were promises.
Note that having several parallel request it a very, very useful, maybe THE most important feature of promises: Who wants to chain then()'s if independent requests can be satisfied at the same time!
Ok, jQuery's $.when().then() is far from perfect, IMHO. But at least there should be good documentation about these issues with good examples.
Motto: If you don't do it right, don't be ashamed of it and try hide it, but tell us how to work around it. That's what is great about jQuery - making W3C, MS, etc. wrongs right and giving us workarounds when it's too hard to make it right.
My 2c.
Jan
Thanks,
Jan
Dr. Jan Stelovsky, Founder and CTO
www.parwinr.com, jan AT parwinr.com, +1-808-673-0035 2903 Bunker Hill Ln #150, Santa Clara, CA 95054
This electronic message transmission contains information from the Company parWinr, Inc. that may be proprietary, confidential and/or privileged. The information is intended only for the use of the individual(s) or entity named above. If you are not the intended recipient, be aware that any disclosure, copying or distribution or use of the contents of this information is prohibited. If you have received this electronic transmission in error, please notify the sender immediately by replying to the address listed in the "From:" field.
On Thu, Feb 18, 2016 at 9:00 AM, Dave Methvin notifications@github.com wrote:
Also, .val() can be used to find out which - if any - radio button in a group is checked. ... $('input[name=radios]:checked').val()
The finding out is all being done by the selector though so it's not about .val(). That is, var checks = $('input[name=radios]:checked') will select all the checked radios, and there may be none. If you wanted to know if any were checked you could just look for checks.length === 0.
Reading the title again, I think you're asking what $(),val() returns for an empty jQuery selection in general, which applies no matter what you were trying to select. As far as I can tell it's returned undefined on an empty selection since at least jQuery 1.7, not null. https://jsfiddle.net/1Lc12g5g/2/
So I see a couple of clarifications we can make here. One, the .val() method returns the value property of the first element in the set, other than the special case select-multiple where it's an array of options. Two, if there is nothing in the set then it returns undefined. Sound good?
— Reply to this email directly or view it on GitHub https://github.com/jquery/api.jquery.com/issues/893#issuecomment-185860806 .
In general, is jQuery interested in such comments/suggestions? ... Frankly, when I face a jQuery problem, I rarely go to jQuery documentation - I always look up stackoverflow first.
The API documentation is first and foremost a description of what inputs an API method takes and what outputs it produces. It is not intended to be a how-to guide, although the examples often provide information on places where that API can be applied. We're open to pull requests that add or clarify wording on the current behavior including edge cases.
The original question you were asking really seemed to be, "How do I determine which radio button is checked?" That is a good StackOverflow question if it isn't clear from the API documentation. All the ingredients are here in the API documentation to discover what needs to be done, including the :checked
selector and several ways to inspect the selected elements. If it's easier to look on StackOverflow for help on putting the ingredients together, then definitely do that.
@dmethvin, it seems to me that this issue has been clarified and there isn't anything else to do. Can I close it?
Can you clarify the type of information you would like to see there?
The idea of a checked or selected element is completely independent of its value retrieved by
.val()
, which is why the examples there show how to select a checked element. Is that the concept you think needs to be clarified?