peirix
peirix

Reputation: 37741

Change radiobutton multiple times using jQuery

I can't seem to change between radio buttons correctly using jQuery. Is this a bug in jQuery? Example here: http://jsbin.com/yafik/1/edit

Code from jsbin:

<input type="radio" name="test">
<input type="radio" name="test">
<input type="radio" name="test">
<input type="radio" name="test">

<script>
    $("input").eq(1).attr("checked", true);
    $("input").eq(2).attr("checked", true);
    //$("input").attr("checked", false);
    $("input").eq(1).attr("checked", true);
</script>

You can also try to uncomment the third line: $("input").attr("checked", false);, which I'd expect to just reset all checked attributes.

I would expect it to set the second radiobutton to checked. But if you look at the source code, both the second and third radiobuttons are actually checked.

Upvotes: 0

Views: 186

Answers (1)

Anton
Anton

Reputation: 32581

Use .prop() for boolean values

$("input").eq(1).prop("checked", true);
$("input").eq(2).prop("checked", true);
$("input").eq(1).prop("checked", true);

Upvotes: 1

Related Questions