Bug Tracker

Opened 11 years ago

Closed 10 years ago

Last modified 7 years ago

#3609 closed bug (invalid)

setting val() of radio or checkbox to single value sets "value" instead of "checked"

Reported by: jhohm Owned by:
Priority: major Milestone: 1.3
Component: core Version: 1.2.6
Keywords: Cc:
Blocked by: Blocking:

Description

The documentation of val() says, in the second section for val(val):

"Checks, or selects, all the radio buttons, checkboxes, and select options that match the set of values."

And the example code shows that it works given a single non-Array value to select the option on a non-multiple select:

$("#single").val("Single2");

But this does not work for radio or checkbox inputs. Given this HTML:

<input type="radio" name="ordinal" value="1">First</input> <input type="radio" name="ordinal" value="2">Second</input>

code like this:

$("[@name='ordinal']").val("2");

surprisingly ends up setting the "value" attribute of all the radio buttons to "2" like this:

<input type="radio" name="ordinal" value="2">First</input> <input type="radio" name="ordinal" value="2">Second</input>

instead of setting the "checked" attribute on the radio button with value "2" as expected, like this:

<input type="radio" name="ordinal" value="1">First</input> <input type="radio" name="ordinal" value="2" checked>Second</input>

Patch to provide the expected (and in my opinion documented) behavior attached.

Attachments (1)

jqueryjs-set-radio-single-val.diff (789 bytes) - added by jhohm 11 years ago.
Patch to provide expected behavior for setting value of radio buttons or checkboxes

Download all attachments as: .zip

Change History (3)

Changed 11 years ago by jhohm

Patch to provide expected behavior for setting value of radio buttons or checkboxes

comment:1 Changed 11 years ago by dmethvin

How would you set the value attribute of a checkbox or radio element then?

The current behavior allows you to choose the checkbox/radio by passing in an array with a single element.

comment:2 Changed 10 years ago by dmethvin

Resolution: invalid
Status: newclosed
Note: See TracTickets for help on using tickets.