jQuery – Get all selected checkboxes
The below code shows you how to get all selected checkboxes by using :checked
property.
$('input[type=checkbox]:checked');
Here is an example how to use it:
example.html
<form>
<label>Tech:</label><br>
<input type="checkbox" id="php_tech" value="PHP"><label for="php_tech">PHP</label><br>
<input type="checkbox" id="java_tech" value="Java"><label for="java_tech">Java</label><br>
<input type="checkbox" id="aspnet_tech" value="ASP.NET"><label for="aspnet_tech">ASP.NET</label><br>
<button>Get all checked techs</button>
</form>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script type="text/javascript">
$('button').on('click', function() {
var checkedTechElements = $('input[type=checkbox]:checked');
for (var i = 0; i < checkedTechElements.length; i++) {
console.log(checkedTechElements[i].value);
}
return false;
});
</script>