35

I would like to check the first radio button of each group. But there are some radio button that are disabled, so the script should ignore them and go to next non-disabled button.

I wrote something like this but it doesn't work:

$(document).ready(function(){
if ($("input['radio']).is(':disabled'))
    {  
        $(this).attr('checked', false );
    }
else
    {
        $(this).attr('checked', true );
    }
});

Thanks a lot

THelper
  • 15,333
  • 6
  • 64
  • 104
Matthew
  • 353
  • 1
  • 3
  • 4

10 Answers10

75

If your buttons are grouped by their name attribute, try:

$("input:radio[name=groupName][disabled=false]:first").attr('checked', true);

If they are grouped by a parent container, then:

$("#parentId input:radio[disabled=false]:first").attr('checked', true);
sje397
  • 41,293
  • 8
  • 87
  • 103
  • 3
    If you want the change event to fire, do `.click()` instead – Yarin Sep 22 '13 at 23:51
  • It works. But I also need to trigger the change event once setting true. – Thamarai T Aug 25 '18 at 07:53
  • I tried '.click()' as by @Yarin, but not worked. But I got it. To trigger 'change' event once setting true, $("input:radio[name=groupName]).trigger("change"); works perfectly. – Thamarai T Aug 25 '18 at 08:19
18
$("input:radio[name=groupX]:not(:disabled):first")

This should give you the first non-disabled radio-button from a group...

Šime Vidas
  • 182,163
  • 62
  • 281
  • 385
8

The below does what you want:

$(document).ready(
  function(){
    $('input:radio:first-child').attr('checked',true);
  }
  );

Demo at: JS Bin.

David Thomas
  • 249,100
  • 51
  • 377
  • 410
5

I tested the first answer and it doesn't resolve it. I had to use the following sentence:

jQuery("input:radio[name=groupName]:visible")[0].checked=true;

This check the first visible radio button with name = groupName

Suraj Rao
  • 29,388
  • 11
  • 94
  • 103
Andres
  • 51
  • 1
  • 1
3
<input type="radio" name="r1"><br>
<input type="radio" name="r1"><br>
<hr>
<input type="radio" name="r2"><br>
<input type="radio" name="r2"><br>
<input type="radio" name="r2"><br>

<script>
$(function(){
    //removed duplicates form the following array
    $(jQuery.unique(
        //create an array with the names of the groups
        $('INPUT:radio')
            .map(function(i,e){
                return $(e).attr('name') }
            ).get()
    ))
    //interate the array (array with unique names of groups)
    .each(function(i,e){
        //make the first radio-button of each group checked
        $('INPUT:radio[name="'+e+'"]:visible:first')
            .attr('checked','checked');
    });
});
</script>

jsfiddle = http://jsfiddle.net/v4auT/

Floyd
  • 1,898
  • 12
  • 20
2

A little upgrade for 2020. The official documentation suggests the use of "first()" and "prop()"

$("input[name='groupName'][disabled=false]").first().prop("checked", true);
Kira
  • 608
  • 3
  • 10
  • 22
1

Try this :

 $("input:radio:not(:disabled)").attr("checked", true);

To check the first radio button only in a group you can

 $("parentelement input:radio:not(:disabled):first-child").attr("checked", true);
VijayS91
  • 1,535
  • 23
  • 38
rahul
  • 184,426
  • 49
  • 232
  • 263
1

This makes the first radio checked by default

jQuery("input:radio[name=groupName]").first().click()
Tahir Yasin
  • 11,489
  • 5
  • 42
  • 59
1

If you've the class and you don't bother about disabled items, just do this

$(".className").first().click();

Or simply

$(".className").first().prop("checked", true);
Manu Mohan
  • 1,694
  • 2
  • 20
  • 31
  • Use .click(); to make sure that the selected dot is also visible otherwise prop('checked',true) will not shift the dot – Aditya Feb 17 '22 at 09:26
0
$('.choose_site input:radio').attr('checked',false); //Unchecked All
$('.choose_site input:radio').first().prop("checked", true); // Checked First
Shunya
  • 2,344
  • 4
  • 16
  • 28
Sandeep Sherpur
  • 2,418
  • 25
  • 27