Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set radio button 'checked' in jquery based on ID

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!

like image 221
dzm Avatar asked Jun 25 '10 22:06

dzm


1 Answers

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);
});
like image 151
Sarfraz Avatar answered Sep 22 '22 17:09

Sarfraz