Search code examples
jqueryradio-buttonchecked

Check first radio button with JQuery


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


Solution

  • 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);