Radio buttons with the same name form a group in which only one option can be selected. Use :checked to read the selected value and .prop() to change checked/disabled state.
<label>
<input type="radio"
name="r1"
id="r1"
value="option1"
checked>
Option 1
</label>
<label>
<input type="radio"
name="r1"
id="r2"
value="option2">
Option 2
</label>
const selected =
$( 'input[name="r1"]:checked' ).val();
Demo: read selected radio value →
$( 'input[name="options"]' )
.on( "change", function () {
$( "#d1" ).text(
$( this ).val()
);
});
$( 'input[name="enq_status"]' )
.on( "change", function () {
const params = new URLSearchParams({
enq_status: $( this ).val()
});
window.location.assign(
"radio-button-submit.php?" +
params.toString()
);
});
Full page-reload example with PHP →
$( 'input[type="radio"]' )
.on( "change", function () {
const name = this.name;
const id = this.id;
const group =
$( this ).data( "group" );
console.log( name, id, group );
});
$( 'input[name="options"]' )
.prop( "checked", false );
$( "#radio-1" )
.prop( "checked", true );
The older .attr("checked") compatibility example is no longer recommended for current jQuery; checked state is a property.
$( 'input[name="options"]' )
.prop( "disabled", true );
$( "#opt1" )
.prop( "disabled", false );
$( "#opt1, #opt2" )
.prop( "disabled", true );
$( 'input[name="r1"]' )
.on( "change", function () {
const enabled =
$( this ).val() === "true";
$( "#my_tabs" ).tabs(
"option",
"collapsible",
enabled
);
});
This particular example requires the jQuery UI Tabs widget.
jQuery UI Checkboxradio can style native radio groups. The original tutorial also includes option-group examples:
Demo: radio controls with OPTGROUP-related data →
These original tutorial/demo routes are retained so no useful learner path is lost:
Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.
| Sandra | 23-07-2016 |
| Thank you very much for the demo "Disabling two radio buttons". I spend several days to find out how to add a required attribute to a textarea after a radio button has been checked. This demo helped me to finally find a working code. | |