javascript - How to find the length of checkbox checked inputs? -
this question has answer here:
all find length
of checked checkboxes.
in
codepen
have created sample reference, can able find length while clicking oncheck box
, looking while clicking ofall input
check box
, checkbox should checked.for example: while
click (or) checked
rose input check box result of length shows1
, whenclick (or) checked
inputcheck box
expecting result should4
, when clicking onall check box
inputrose, white, blue
need checked.how while clicking on 1 input check box others check box need checked?.
html:
<div id="checkboxlength" class="css"> <div class="blue"> <input type="checkbox"><span>all</span> </div> <div> <input type="checkbox" ><span>rose</span> </div> <div> <input type="checkbox" ><span>blue</span> </div> <div> <input type="checkbox"><span>white</span> </div> <div class="green"> <span id="count-checked-checkboxes">0</span> checked </div> </div>
java script:
$(document).ready(function(){ var $checkboxes = $('#checkboxlength input[type="checkbox"]'); $checkboxes.change(function(){ var countcheckedcheckboxes = $checkboxes.filter(':checked').length; $('#count-checked-checkboxes').text(countcheckedcheckboxes); $('#edit-count-checked-checkboxes').val(countcheckedcheckboxes); }); });
id:
checkboxlength
result code:
<div> <span id="count-checked-checkboxes">0</span> checked </div>
you can add id
or class
check all
checkbox, can eliminate while counting checked checkboxes , detect while clicked can check/uncheck others checkboxes.
var $checkboxes = $('#checkboxlength input[type="checkbox"]'); $checkboxes.change(function(){ /*if clicked checkbox has check_all id check other checkboxes if 1 checked , vise versa */ if($(this).is("#check_all")) $checkboxes.prop('checked',$(this).prop('checked')); $('#count-checked-checkboxes').text($checkboxes.filter(':checked').length); });
.css{ display:flex; justify-content:center; align-items:center; flex-direction:column; } .green{ color:red; } .blue{ color:blue; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script> <div id="checkboxlength" class="css"> <div class="blue"><input type="checkbox" id="check_all"><span>all</span></div> <div><input type="checkbox" ><span>rose</span></div> <div><input type="checkbox" ><span>blue</span> </div> <div><input type="checkbox"><span>white</span> </div> <div class="green"><span id="count-checked-checkboxes">0</span> checked</div> </div>
Comments
Post a Comment