Andrey
Andrey

Reputation: 21285

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.

Upvotes: 3

Views: 6181

Answers (6)

Kevin LaBranche
Kevin LaBranche

Reputation: 21088

This is not a bug but a change:

http://christierney.com/2011/05/06/understanding-jquery-1-6s-dom-attribute-and-properties/

Also, as mentioned by @Neal they have worked on this a bit in the latest 1.6.1 release candidate.

From the RC link:

Upgrading From 1.5.2 to 1.6.1 - With the introduction of the new .prop() method and the changes to the .attr() method, jQuery 1.6 sparked a discussion about the difference between attributes and properties and how they relate to each other. It also came with some backwards compatibility issues that have been fixed in 1.6.1. When updating from 1.5.2 to 1.6.1, you should not have to change any code.

There's a lot more explanation there but you might be able to skip to 1.6.1 and be fine...

EDIT - Added below on 5/16/11

John Resig just weighed in on the changes made around this and why.... Good read....

http://ejohn.org/blog/jquery-16-and-attr/

Upvotes: 3

Edgar Villegas Alvarado
Edgar Villegas Alvarado

Reputation: 18344

.attr() and .data() have changed dramatically in jQuery 1.6.

It's better explained on this article:

Upgrading to jQuery 1.6: Problems you may face

Hope this helps. Cheers

Upvotes: 0

Naveed Ahmad
Naveed Ahmad

Reputation: 3175

You can hack it this way: jQuery("input[name='test']:checked")

The demo:

http://jsfiddle.net/8Eqpu/15/

Upvotes: 0

Michael Levy
Michael Levy

Reputation: 13297

I can't explain the change between versions, but there is a selector specifically looking for checked - http://api.jquery.com/checked-selector/

Upvotes: 0

Xavier Holt
Xavier Holt

Reputation: 14619

I've been seeing this too. The other answers have some insights as to why this is, and when it'll be reverted (only for getters?); in the meantime, I've been using

$('#thingy').is(':checked');

as a cross-version workaround.

Hope this helps!

Upvotes: 3

Naftali
Naftali

Reputation: 146310

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

Upvotes: 8

Related Questions