#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)
Change History (3)
Changed 14 years ago by
Attachment: | jqueryjs-set-radio-single-val.diff added |
---|
comment:1 Changed 14 years ago by
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 14 years ago by
Resolution: | → invalid |
---|---|
Status: | new → closed |
Patch to provide expected behavior for setting value of radio buttons or checkboxes