views:

37

answers:

1

I have two radio buttons with the same name, one is checked by default. How can you check or uncheck a radio button in jQuery when selecting from id?

I've tried:

$('#radio1').attr('checked','checked');
$('#radio1').attr('checked', true);

Nothing seems to work.. any ideas?

Thank you!

+2  A: 

You can not have same id (#radio1) more than once, use a class instead.

$('.radio1').attr('checked', true);
$('.radio2').attr('checked', true);

The id should be used once per element per page.

If you want to check/uncheck on click however, you may do like:

$('#someid').click(function(){
  $('#radio1').attr('checked', true);
});

Or

$('#someid').click(function(){
  $('#radio1').attr('checked', this.checked);
});
Sarfraz