0

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.

peirix
  • 36,512
  • 23
  • 96
  • 126
  • Put your code here. JSBin is not a substitution. And Read http://stackoverflow.com/questions/5874652/prop-vs-attr – Satpal Apr 14 '14 at 12:58
  • Your jsBin works as expected, so what are you expecting??? – A. Wolff Apr 14 '14 at 13:01
  • @A.Wolff What browser are you using? It doesn't work in Chrome. Seems to be working in Firefox, though. – peirix Apr 14 '14 at 13:02
  • 1
    @peirix on chrome too, works for me as expected, only second radio button is checked – A. Wolff Apr 14 '14 at 13:04
  • See also : [Properties and attributes in HTML](http://stackoverflow.com/questions/6003819/properties-and-attributes-in-html) – LeGEC Apr 14 '14 at 13:11

1 Answers1

1

Use .prop() for boolean values

$("input").eq(1).prop("checked", true);
$("input").eq(2).prop("checked", true);
$("input").eq(1).prop("checked", true);
Anton
  • 32,245
  • 5
  • 44
  • 54