:checked
selectorYou can use the jQuery :checked
selector in combination with the each()
method to retrieve the values of all checkboxes selected in a group. The each()
method used here simply iterates over all the checkboxes that are checked. Let's try out an example to see how it works:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>jQuery Get Values of Selected Checboxes</title>
<script src="https://code.jquery.com/jquery-1.12.4.min.js"></script>
<script>
$(document).ready(function() {
$("button").click(function(){
var favorite = [];
$.each($("input[name='sport']:checked"), function(){
favorite.push($(this).val());
});
alert("My favourite sports are: " + favorite.join(", "));
});
});
</script>
</head>
<body>
<form>
<h3>Select your favorite sports:</h3>
<label><input type="checkbox" value="football" name="sport"> Football</label>
<label><input type="checkbox" value="baseball" name="sport"> Baseball</label>
<label><input type="checkbox" value="cricket" name="sport"> Cricket</label>
<label><input type="checkbox" value="boxing" name="sport"> Boxing</label>
<label><input type="checkbox" value="racing" name="sport"> Racing</label>
<label><input type="checkbox" value="swimming" name="sport"> Swimming</label>
<br>
<button type="button">Get Values</button>
</form>
</body>
</html>
Hi, My name is Harsukh Makwana. i have been work with many programming language like php, python, javascript, node, react, anguler, etc.. since last 5 year. if you have any issue or want me hire then contact me on [email protected]
How to determine if variable is undefined or null in JavaScript
Use the equality operator (==) In Jav...VueJs CRUD using Google Firebase
This is a step by step tutorial that exp...How to add elements to the beginning of an array in PHP
Use the PHP array_unshift() fu...How to Create Custom Directive Attribute in Angular9?
Angular Directives is basicall...How to add elements to the end of an array in PHP
Use the PHP array_push() funct...