I've been trying to programatically select radio buttons with jQuery, something I thought would be as simple as changing the checked attribute.
However, the following code doesn't seem to do what is expected in jQuery 1.9.1 in Chrome/Firefox.
Expected behaviour: Click the div enclosing the radio button -> 'checked' attribute gets set -> renders checked in the DOM.
Actual behaviour: Click the div enclosing the radio button -> 'checked' attribute gets set -> renders checked in the DOM for the first and second button clicked, subsequent buttons don't render as checked.
jQuery:
$('div.form-type-radio').on('click', function () {
var Id = $(this).find('input[type=radio]').attr('id');
$('form input[type=radio]:not(#'+Id+')').removeAttr('checked');
$('#' + Id).attr('checked', 'checked');
console.log($('#' + Id));
});
Here's a jsFiddle - http://jsfiddle.net/GL9gC/
I've tried the same code with previous versions of jQuery and it all works as expected.
not
works with elements as well). Your entire function could be just$('input[type=radio]', this).prop('checked', true);
– Kassandra