jQuery

How to check and uncheck a radio button using jQuery

In this tutorial, we are going to see how to check and uncheck a radio button using jQuery. You can easily use the method prop() of jQuery to check / uncheck a radio button dynamically, for example by clicking a button. The prop() method requires a version of jQuery > 1.6.
 

How to check and uncheck a radio button using jQuery
<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title>Check and uncheck a radio button</title>
		<script src="https://code.jquery.com/jquery-1.12.4.min.js"></script>
		<script type="text/javascript">
			$(document).ready(function(){
				$(".check-on").click(function(){
					$("#on").prop("checked", true);
				});
				$(".check-off").click(function(){
					$("#off").prop("checked", true);
				});
			});
		</script>
	</head>
	<body>
		<label><input type="radio" name="state" id="on"> On</label>
		<label><input type="radio" name="state" id="off"> Off</label>
		<br>
		<button type="button" class="check-on">Enable</button>
		<button type="button" class="check-off">Disable</button>  
	</body>
</html>
Result

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 *