jQuery

How to get the value of selected radio button with jQuery

In this tutorial, we are going to see how to get the value of selected radio button with jQuery. You can easily use the selector :checked of jQuery with the method val() to get the value of a radio button. Here is an example:
 

How to get the value of selected radio button with jQuery
<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title>Get the value of selected radio button</title>
		<script src="https://code.jquery.com/jquery-1.12.4.min.js"></script>
		<script type="text/javascript">
			$(document).ready(function(){
				$("input[type='button']").click(function(){
					var val = $("input[name='langage']:checked").val();
					if(val){
						alert("Your preferred programming language is : " + val);
					}
				});
				
			});
		</script>
	</head> 
	<body>
		<h5>What is your favorite programming language?</h5>
		<p> 
			<label><input type="radio" name="langage" value="javascript">JavaScript</label><br> 
			<label><input type="radio" name="langage" value="python">Python</label><br>
			<label><input type="radio" name="langage" value="php">PHP</label><br>
			<label><input type="radio" name="langage" value="java">Java</label><br>
			<label><input type="radio" name="langage" value="scala">Scala</label>
		</p>
		<input type="button" value="Get the value">
	</body>
</html>
Result
What is your favorite programming language?





mcqMCQPractice competitive and technical Multiple Choice Questions and Answers (MCQs) with simple and logical explanations to prepare for tests and interviews.Read More

Leave a Reply

Your email address will not be published. Required fields are marked *