3

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.

Naftali
  • 144,921
  • 39
  • 244
  • 303
Andrey
  • 20,487
  • 26
  • 108
  • 176

6 Answers6

8

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

Community
  • 1
  • 1
Naftali
  • 144,921
  • 39
  • 244
  • 303
3

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/

Kevin LaBranche
  • 20,908
  • 5
  • 52
  • 76
  • Thank you for using my source? – Naftali May 11 '11 at 19:40
  • @Neal - I wanted to attribute you linking to it first. Isn't it better to reference you instead of just writing what I did without mentioning that you already did....? Certainly meant no disrepect. :-) – Kevin LaBranche May 11 '11 at 19:44
  • lol ook. just might be better suited as a comment, rather than a fully `new` answer :-p – Naftali May 11 '11 at 19:45
  • @Neal - I already had answered and added to my answer with the quote to let @Andrey know he might be able to skip right to the RC to fix his issue. Your answer didn't state that.... :-) I did +1 you also. :-) – Kevin LaBranche May 11 '11 at 19:49
  • @Neal - Yes. "Fixed some of the 1.6 attr problems." But what & more specifically the post states you can go to 1.6.1 from 1.5.2 to help avoid the issues. I wanted to point this out to @Andrey and anyone else since it is directly tied to solving his issue. – Kevin LaBranche May 11 '11 at 19:57
3

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!

Xavier Holt
  • 14,471
  • 4
  • 43
  • 56
0

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

Michael Levy
  • 13,097
  • 15
  • 66
  • 100
0

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

The demo:

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

Naveed Ahmad
  • 3,176
  • 1
  • 15
  • 18
0

.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

Edgar Villegas Alvarado
  • 18,204
  • 2
  • 42
  • 61