We can use jQuery to set the value of a radio button to checked by making use of the jQuery prop() method. We simply need to change the checked property of the radio button to true. Here is how we can do this:
$("#radio-button").prop("checked",true);
If using WordPress, don’t forget to change the $ to jQuery:
jQuery("#radio-button").prop("checked",true);
Let’s see an example of this below.
Lets say we have the following HTML:
<div id="div1">
<p>Select your favorite mode of transportation:</p>
<input type="radio" id="bike" name="transport" value="Bike"> <label for="bike">Bike</label><br>
<input type="radio" id="car" name="transport" value="Car"> <label for="car">Car</label><br>
<input type="radio" id="bus" name="transport" value="Bus"> <label for="bus">Bus</label><br>
<input type="radio" id="train" name="transport" value="Train"> <label for="train">Train</label><br>
<input type="radio" id="boat" name="transport" value="Boat"> <label for="boat">Boat</label><br>
<input type="radio" id="airplane" name="transport" value="Airplane"> <label for="airplane">Airplane</label><br>
</div>
<div id="click-me" onclick="setRadio()">Set the radio button Airplane to checked</div><br>
</div>
In this example, we will use the onclick event to call a function setRadio() which we will create below.
The setRadio() function will simply set the last radio button, Airplane, to be checked.
Here is our function using jQuery:
function setRadio(){
$("#airplane").prop("checked",true);
};
Let’s try this out below:
Code Output:
Select your favorite mode of transportation:
Full Code:
<div id="div1">
<p>Select your favorite mode of transportation:</p>
<input type="radio" id="bike" name="transport" value="Bike"> <label for="bike">Bike</label><br>
<input type="radio" id="car" name="transport" value="Car"> <label for="car">Car</label><br>
<input type="radio" id="bus" name="transport" value="Bus"> <label for="bus">Bus</label><br>
<input type="radio" id="train" name="transport" value="Train"> <label for="train">Train</label><br>
<input type="radio" id="boat" name="transport" value="Boat"> <label for="boat">Boat</label><br>
<input type="radio" id="airplane" name="transport" value="Airplane"> <label for="airplane">Airplane</label><br>
</div>
<div id="click-me" onclick="setRadio()">Set the radio button Airplane to checked</div><br>
</div>
<script>
function setRadio(){
$("#airplane").prop("checked",true);
};
</script>
Using jQuery to Get the Value of a Radio Button
We can use jQuery to get the value of a radio button using the jQuery val() method.
$('input[name="radio1"]:checked').val();
Let’s see an example of this below.
Lets say we have the following HTML:
<div id="div2">
<p>Select your favorite mode of transportation:</p>
<input type="radio" id="bike1" name="transport" value="Bike"><label for="bike1">Bike</label><br>
<input type="radio" id="car1" name="transport" value="Car"><label for="car1">Car</label><br>
<input type="radio" id="bus1" name="transport" value="Bus"><label for="bus1">Bus</label><br>
<input type="radio" id="train1" name="transport" value="Train" checked><label for="train1">Train</label><br>
<input type="radio" id="boat1" name="transport" value="Boat"><label for="boat1">Boat</label><br>
<input type="radio" id="airplane1" name="transport" value="Airplane"><label for="airplane1">Airplane</label><br>
</div>
In this example, the checked radio button to start will be the Train option. We can use our code above to get this value using jQuery.
Here is the jQuery code we will need:
var selectedRadioButton = $('#div2 input[name="transport"]:checked').val();
Try it out below by selecting a radio button and then clicking the button below to see what radio button is checked:
Code Output:
Select your favorite mode of transportation:
Full Code:
<div id="div2">
<p>Select your favorite mode of transportation:</p>
<input type="radio" id="bike1" name="transport" value="Bike"> <label for="bike1">Bike</label><br>
<input type="radio" id="car1" name="transport" value="Car"> <label for="car1">Car</label><br>
<input type="radio" id="bus1" name="transport" value="Bus"> <label for="bus1">Bus</label><br>
<input type="radio" id="train1" name="transport" value="Train" checked> <label for="train1">Train</label><br>
<input type="radio" id="boa1t" name="transport" value="Boat"> <label for="boat1">Boat</label><br>
<input type="radio" id="airplane1" name="transport" value="Airplane"> <label for="airplane1">Airplane</label><br>
</div>
<div id="click-me" onclick="selectedRadio()">Check which radio button is selected with jQuery</div>
<div id="results"></div>
</div>
<script>
function selectedRadio(){
var selectedRadioButton = $('#div2 input[name="transport"]:checked').val();
$("#results").text(selectedRadioButton);
};
</script>
Hopefully this article has been useful to help you understand how to use jQuery to set a radio button to checked.
Leave a Reply