Search code examples
jqueryjquery-1.6

Possible bug in jQuery 1.6 - $(...).attr("checked") is not working


I have two radio buttons on my form and up until I started using jQuery 1.6 the following code worked fine:

<input type="radio" id="radio1" name="test"/>
<input type="radio" id="radio2" name="test"/>
<input type="button" onclick="testcheck()" value="Test"/>
<script>
function testcheck()
{
    if (jQuery("#radio1").attr("checked"))
        alert("first button checked");
    else if (jQuery("#radio2").attr("checked"))
        alert("second button checked");
    else
        alert("none checked")      
}
</script>

Once I start using jQuery 1.6, it always shows "none checked" because jQuery(radiobutton).attr("checked") is always empty.

Take a look at this jsfiddle, and change jQuery version between 1.5.2 and 1.6 to see what I mean.


Solution

  • Take a look at this question: .prop() vs .attr()

    Try this for your code instead:

    function testcheck()
    {
        if (jQuery("#radio1").prop("checked"))
            alert("first button checked");
        else if (jQuery("#radio2").prop("checked"))
            alert("second button checked");
        else
            alert("none checked")      
    }
    

    Also in the newest jQuery 1.6.1 they fixed some of the 1.6 attr problems