Set "checked" switch in jquery based on id

I have two radio buttons with the same name, by default - one at a time. How can you check or uncheck with a switch in jQuery when choosing from id?

I tried:

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

Nothing works .. any ideas?

Thank!

+5
source share
3 answers

You cannot have the same identifier ( #radio1) more than once, use instead class.

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

id should be used once for each element on the page.

If you want to check / uncheck the box click, you can do the following:

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

or

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

, :

<input type="radio" id="radio1" name="same-radio-name" />
<input type="radio" id="radio2" name="same-radio-name" />

, :

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

Viceversa:

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

: JQuery UI? , , :

$('#radio1').attr('checked', true).button("refresh");
+7

, , :

 var selValue = $('input[name=rbnNumber]:checked').val();

"selValue", "rbnNumber". , .

-1

All Articles