I have a set of checkboxes and want to concatenate only the checked ones into a query string, but for some reason val() only returns the first one. The jQuery API docs mention that selects can return an array form the .val() function, can I get this same behavior for inputs as well, with a prototype or jquery plugin?
Here's my code:
$('.boxes input:checked');
// [ <input type="checkbox" value="val1">, <input type="checkbox" value="val2">, <input type="checkbox" value="val3"> ]
$('.boxes input:checked').val();
// "val1"
Ideally the second console output would be:
$('.boxes input:checked').val();
// [ "val1", "val2", "val3" ]
val
returns an array of selected values forselect
elements that havemultiple
attribute, for other elements it returns the value of the first element in the set. You can use themap
method:You can also define a method: