jQuery

How to check and uncheck a checkbox with a button using jQuery

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

How to check and uncheck a checkbox with a button using jQuery
<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title>Check and uncheck a checkbox with a button</title>
		<script src="https://code.jquery.com/jquery-3.2.1.min.js"></script>
		<script type="text/javascript">
			$(document).ready(function(){
				$(".check").click(function(){
					$("#myCheckbox").prop("checked", true);
				});
				$(".uncheck").click(function(){
					$("#myCheckbox").prop("checked", false);
				});
			});
		</script>
	</head>
	<body>
		<p><input type="checkbox" id="myCheckbox"> Click on the button to check / uncheck the Checkbox.</p>
		<button type="button" class="check">Check</button>
		<button type="button" class="uncheck">Uncheck</button>
	</body>
</html>
Result

Click on the button to check / uncheck the Checkbox.

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 *