Bug Tracker

Ticket #1069 (closed enhancement: worksforme)

Opened 9 years ago

Last modified 5 years ago

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:


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:1 Changed 7 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 7 years ago by sunsean

You can do that via the following:

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

comment:3 Changed 7 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 7 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 5 years ago by dmethvin

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

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.