jQuery: Finds all elements that are checked or selected
jQuery Fundamental - II : Exercise-55
Finds all elements that are checked or selected
Sample solution :
HTML Code :
<!DOCTYPE html>
<html>
<head>
<script src="https://code.jquery.com/jquery-git.js"></script>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>Finds all elements that are checked or selected.</title>
</head>
<body>
<form>
<input type="checkbox" name="color" value="Bike">Red
<input type="checkbox" name="color" value="Car">Green
<input type="checkbox" name="color" value="Car">Blue
<input type="checkbox" name="color" value="Car">Black
</form>
<p></p>
</body>
</html>
JavaScript Code :
var count_Checked = function() {
var n = $( "input:checked" ).length;
$( "p" ).text( n + (n === 1 ? " is" : " are") + " checked." );
};
count_Checked();
$("input[type=checkbox]" ).on( "click", count_Checked );
See the Pen jquery-fundamental-exercise-55 by w3resource (@w3resource) on CodePen.
Contribute your code and comments through Disqus.
Previous: Finds all checkbox inputs.
Next: Finds the checked radio input.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics