Ticket #1069 (closed enhancement: worksforme)
Please extend val()
| Reported by: | daemach | Owned by: | |
|---|---|---|---|
| Priority: | minor | Milestone: | 1.1.3 |
| Component: | core | Version: | 1.1.2 |
| Keywords: | Cc: | ||
| Blocking: | Blocked by: |
Description
At the moment, val() only works properly for single elements. It would be very useful if, when passing val() an array of elements created by using a selector like $('[@name=mycheckboxes]'), it could return an array of values for checkboxes and multi-selects. That is the only feature in the forms plugin I would use so I hate adding the overhead for just this function.
Change History
comment:2 Changed 5 years ago by sunsean
You can do that via the following:
$('[name="mycheckboxes"]').map(function(){ return this.checked; });
comment:3 Changed 4 years ago by dmethvin
Most likely, you are not looking for the value of the checkbox but the checked state? In that case you'd want either sunsean's solution or use something like $("[name=mycheckboxes]:checked") and process them.
comment:4 Changed 4 years ago by pfof
yes, i agree with this suggestion. val() is confusing, given that it returns arrays for single elements and single elements for an array of elements.
$('one, two').val() should return the values of both elements, based on the way almost every other function behaves.
if not that, how about vals() (plural) in the core?
Please follow the bug reporting guidlines and use jsFiddle when providing test cases and demonstrations instead of pasting the code in the ticket.

I think you can use the batch plugin by Brandon Aaron, it's very small (not like jQuery.form) and will solve this.