Updated March 28, 2023
Introduction to jQuery Radio Change
jQuery Radio change event is used when there is a list of two or more mutually exclusive options and the user must select exactly one option. radio change event works for clicking a non-selected radio button will deselect whatever other button was previously chosen in the list. When the value and information of radio buttons change using jquery technology, the radio change event happens. The radio change method either triggers the change event or connects the radio button to execute when a change event happens.
Overview of jQuery Radio Change
The input element with radio type contains in the html form. The form input element selects a particular button manually. If you want to change the radio button value using the event function, then use the event and function. When you can select a radio button, the change event is triggered.
Syntax:
Radio button with function syntax shows below for radio change.
<form id = "radio_box">
<input type = "radio" id = "email" name = "contcts" value = "Email ID">
<label for = "email"> Email ID </label>
</form>
radio change syntax is shown below:
<script>
$(document).ready(function(){
$('#radio_box').change(function(){
selected_value = $("input[name='contacts']:checked").val();
alert(selected_value);
});
</script>
Explanation:
- The change() method uses to display the required function or shows required data after clicking the radio button.
- The input tag element includes a radio button for user interaction.
How to Use jQuery Radio Change?
The following steps are used to create radio change function in the web page.
- Step 1: Use online link or file in the head section of the html file.
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
- Step 2: Use the form to create a radio button.
<form id = "radio_box"> Write form elements along with Radio button. </form>
- Step 3: Create a radio button with its value.
<input type = "radio" id = "mobile" name = "contacts" value = "Mobile Number" onchange = "alert(this.value)">
<label for = "mobile"> Mobile Number </label>
- Step 4: Put all radio buttons’ names similarly to apply the radio change function.
- Step 5: Use a script tag either in the head section or in the body section.
- Step 6: Use the jquery document function.
- Step 7: Apply radio change function inside of the radio change function.
$('#radio_box').change(function(){
selected_value = $("input[name = 'contacts']:checked").val();
alert(selected_value);
});
- This step helps to operate the radio change function. You can either use a direct input tag or use the class or id of the element.
jQuery Radio Detects the Change
The radio button changes detect using the change () method in the jquery script tag.
There are requiring some events and elements in the radio button.
- Create input tag with radio button type for user interaction.
- All radio types require a similar name inside of the input tag.
- Use the “onchange” element with alert value.
Code:
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
</head>
<body>
<form id="radio_box">
<input type = "radio" id = "mobile" name = "contacts" value = "Mobile Number">
<label for = "mobile"> Mobile Number </label>
</form>
<script>
$(document).ready(function(){
$('#radio_box').change(function(){
selected_value = $("input[name='contacts']:checked").val();
alert(selected_value);
});
});
</script>
</body>
</html>
Examples of jQuery Radio Change
Given below are the examples mentioned:
Example #1
Basic radio change example and output is shown below.
Code:
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
</head>
<body>
<h4> jquery Radion Change function </h4>
<form id="radio_box">
<input type = "radio" id = "email" name = "contacts" value = "Email ID" onchange = "alert(this.value)">
<label for = "email"> Email ID </label>
<input type = "radio" id = "user" name = "contacts" value = "User ID" onchange = "alert(this.value)">
<label for = "user"> User ID </label>
<input type = "radio" id = "mobile" name = "contacts" value = "Mobile Number" onchange = "alert(this.value)">
<label for = "mobile"> Mobile Number </label>
</form>
<script>
$(document).ready(function(){
$("input").change();
});
</script>
</body>
</html>
Output:
Example #2
radio button change example with variable and alert box.
Code:
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
</head>
<body>
<h4> jquery Radion Change function </h4>
<form id="radio_box">
<input type = "radio" id = "email" name = "contacts" value = "Email ID">
<label for = "email"> Email ID </label>
<input type = "radio" id = "user" name = "contacts" value = "User ID>
<label for = "user"> User ID </label>
<input type = "radio" id = "mobile" name = "contacts" value = "Mobile Number">
<label for = "mobile"> Mobile Number </label>
</form>
<script>
$(document).ready(function(){
$('#radio_box').change(function(){
selected_value = $("input[name='contacts']:checked").val();
alert(selected_value);
});
});
</script>
</body>
</html>
Output:
Example #3
radio button change example without variable and alert box.
Code:
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
</head>
<body>
<h4> jquery Radion Change function </h4>
<form id="radio_box">
<input type = "radio" id = "email" name = "contacts" value = "Email ID">
<label for = "email"> Email ID </label>
<input type = "radio" id = "user" name = "contacts" value = "User ID>
<label for = "user"> User ID </label>
<input type = "radio" id = "mobile" name = "contacts" value = "Mobile Number">
<label for = "mobile"> Mobile Number </label>
</form>
<script>
$(document).ready(function(){
$('#radio_box').change(function(){
alert($("input[name='contacts']:checked").val());
});
});
</script>
</body>
</html>
Output:
Example #4
radio button change example with display output value.
Code:
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
</head>
<body>
<h4> jquery Radion Change function </h4>
<form id="radio_box">
<input type = "radio" id = "email" name = "contacts" value = "Email ID">
<label for = "email"> Email ID </label>
<input type = "radio" id = "user" name = "contacts" value = "User ID">
<label for = "user"> User ID </label>
<input type = "radio" id = "mobile" name = "contacts" value = "Mobile Number">
<label for = "mobile"> Mobile Number </label>
</form>
<script>
$(document).ready(function(){
$('#radio_box').change(function(){
selected_value = $("input[name='contacts']:checked").val();
document.write(selected_value);
});
});
</script>
</body>
</html>
Output:
Example #5
radio button change example with the “if else” condition.
Code:
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
</head>
<body>
<h4> jquery Radion Change function </h4>
<form id="radio_box">
<input type = "radio" id = "email" name = "contacts" value = "Email ID">
<label for = "email"> Email ID </label>
<input type = "radio" id = "user" name = "contacts" value = "User ID">
<label for = "user"> User ID </label>
<input type = "radio" id = "mobile" name = "contacts" value = "Mobile Number">
<label for = "mobile"> Mobile Number </label>
</form>
<script>
$(document).ready(function(){
$('input[type=radio][name=contacts]').change(function(){
if (this.value == "Email ID") {
alert("Select Email Id");
}else if (this.value == "User ID") {
alert("Select User Id");
}
else if (this.value == "Mobile Number") {
alert("Select Mobile Number");
}
});
});
</script>
</body>
</html>
Output:
Conclusion
The radio change function uses to detect the change of the radio button values. This function works for user interaction with web page form. It selects a single value from multiple options using the change function.
Recommended Articles
This is a guide to jQuery Radio Change. Here we discuss the introduction, how to use it and examples respectively. You may also have a look at the following articles to learn more –