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
We can check the status of a radio button by using the :checked jQuery selector together with the jQuery function is . For example: $('#el').is(':checked') . It is exactly the same method we use to check when a checkbox is checked using jQuery.
To select a radio button by default we have to check it as true. If it is checked as true then by default it will be autofocused. In the following example, we have only radio buttons and none of them is autofocused.
The simplest way is to use the :first-child selector. As opposed to :first , which only returns the first of the matched elements, :first-child will return any element that is the first child of its parent element: //returns the first radio button in group1, and the first one in group2 as well.
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);
$("input:radio[name=groupX]:not(:disabled):first")
This should give you the first non-disabled radio-button from a group...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With