in jQuery

JQuery 1.6+ attr and prop issue

I used the following code to make sure all radio buttons where unchecked and the current one was set correctly. This worked well before jQuery 1.6.

1
2
3
4
5
6
7
8
$(document).ready(function() {
SetUniqueRadioButton = function(current, id) {
jQuery("input[type=radio]").each(function() {
$(this).attr("checked", false);
});
current.checked = true;
};
});

Since jQuery 1.6, this code did not work and after doing some digging around, I found that you need to use the new method .prop() to get the code functioning correctly again.

1
2
3
4
5
6
7
8
$(document).ready(function() {
SetUniqueRadioButton = function(current, id) {
jQuery("input[type=radio]").each(function() {
$(this).prop("checked", false);
});
current.checked = true;
};
});

Before jQuery 1.6, the .attr() method sometimes took property values into account when retrieving some attributes, which could cause inconsistent behavior. As of jQuery 1.6, the .prop() method provides a way to explicitly retrieve property values, while .attr() only retrieves attributes.

References:

http://api.jquery.com/prop/