Check first radio button with JQuery

Viewed 78916

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

10 Answers

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

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

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

This makes the first radio checked by default

jQuery("input:radio[name=groupName]").first().click()

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);
$('.choose_site input:radio').attr('checked',false); //Unchecked All
$('.choose_site input:radio').first().prop("checked", true); // Checked First
Related