In this tutorial we will learn how to Get Checked Radio Button Value By Name in jQuery. jQuery val() method, Attribute Equals Selector and :checked Selector are used to get the value of checked radio button by it's name.
Table of Contents
jQuery val() Method
jQuery val() Method sets or returns the value of input HTML elements.
In this example val() method is used to get the value of checked radio button using it's name.
Attribute Equals Selector
Attribute Equals Selector is a selector used to select HTML elements whose defined attributes have exact defined values.
In this example Attribute Equals Selector is [name=color].
:checked Selector
:checked Selector is used to target or select the checked radio buttons and checkboxes.
In this example :checked Selector is used to select the checked radio button to get it's value.
HTML Code
HTML Code is given below, in this code we have two radio buttons with two labels and a button tag.
<!DOCTYPE html>
<html>
<head>
<title>Get Checked Radio Button Value By Name jQuery</title>
<script src="https://code.jquery.com/jquery-3.3.1.min.js"></script>
</head>
<body>
<label for="r1">Red</label>
<input id="r1" type="radio" value='red' name="color">
<label for="b1">Blue</label>
<input id="b1" type="radio" value='blue' name="color">
<button id='btn'>Check Radio Value</button>
<script src="script.js"></script>
</body>
</html>
JQuery Code
JQuery Code is given below, the code is executed whenever button is clicked. This is done using jQuery click() method.
$('input[name=color]:checked') is used to select the checked radio button with specific name. While val() method is used to get the value of that checked radio button.
<script>
$(document).ready(function(){
$('#btn').click(function(){
var value = $('input[name=color]:checked').val();
alert(value);
});
});
</script>
Demo
Video Tutorial
Watch video tutorial on How To Get Checked Radio Button Value By Name in jQuery.