Bug Tracker

Opened 10 years ago

Closed 6 years ago

#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:
Blocked by: Blocking:


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 (5)

comment:1 Changed 9 years ago by flesler

  • Priority changed from major to minor

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

comment:2 Changed 9 years ago by sunsean

You can do that via the following:

 $('[name="mycheckboxes"]').map(function(){ return this.checked; });

comment:3 Changed 8 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 8 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?

comment:6 Changed 6 years ago by dmethvin

  • Resolution set to worksforme
  • Status changed from new to closed

Several good and short solutions have been proposed here as alternatives that work with the existing jQuery versions.

Note: See TracTickets for help on using tickets.