Here, we will use the combination of the value() method and :checked selector method to find the value of the selected radio button in the group of elements. Follow the below example.
Example:
<!DOCTYPE html>
<html>
<head>
<title>Jquery get value of radio button</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css">
<link rel="stylesheet" href="style.css">
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
</head>
<body>
<section>
<div class="container">
<h2>Select your product.</h2>
<p>
<label><input type="radio" name="product" value="1"> Product 1</label>
<label><input type="radio" name="product" value="2"> Product 2</label>
<label><input type="radio" name="product" value="3"> Product 3</label>
</p>
<p><input type="button" value="Get The Value"></p>
</div>
</section>
<script>
$(document).ready(function(){
$("input[type='button']").click(function(){
var radioVal = $("input[name='product']:checked").val();
if(radioVal){
alert("Product - " + radioVal);
}
});
});
</script>
</body>
</html>
Add the below stylesheet to the html file for better responsive design.
body {
margin: 50px;
font-family: Arial, Helvetica, sans-serif;
}
label{padding-top: 20px;}
Total Views: 1,010