I have a list of checkbox in AEM When we hit the checkbox the checked attribute will be placed on the parent element, not in the input box. I want the count how many are checked
console.log(
$('.isCorrectanswer').attr('checked').length
);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<coral-checkbox class="isCorrectanswer" checked>
<input type="checkbox" name="./checkboxone" />
<label>checkbox</label>
</coral-checkbox>
<coral-checkbox class="isCorrectanswer" checked>
<input type="checkbox" name="./checkboxtwo" />
<label>checkbox</label>
</coral-checkbox>
<coral-checkbox class="isCorrectanswer">
<input type="checkbox" name="./checkboxthree" />
<label>checkbox</label>
</coral-checkbox>
<coral-checkbox class="isCorrectanswer">
<input type="checkbox" name="./checkboxfour" />
<label>checkbox</label>
</coral-checkbox>
<coral-checkbox class="isCorrectanswer">
<input type="checkbox" name="./checkboxfive" />
<label>checkbox</label>
</coral-checkbox>
This is not working for me...
Use the .isCorrectanswer[checked]
selector:
console.log(
$('.isCorrectanswer[checked]').length
);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<coral-checkbox class="isCorrectanswer" checked>
<input type="checkbox" name="./checkboxone" />
<label>checkbox</label>
</coral-checkbox>
<coral-checkbox class="isCorrectanswer" checked>
<input type="checkbox" name="./checkboxtwo" />
<label>checkbox</label>
</coral-checkbox>
<coral-checkbox class="isCorrectanswer">
<input type="checkbox" name="./checkboxthree" />
<label>checkbox</label>
</coral-checkbox>
<coral-checkbox class="isCorrectanswer">
<input type="checkbox" name="./checkboxfour" />
<label>checkbox</label>
</coral-checkbox>
<coral-checkbox class="isCorrectanswer">
<input type="checkbox" name="./checkboxfive" />
<label>checkbox</label>
</coral-checkbox>
(Side note: for the sake of naming consistency, you might consider renaming the class to isCorrectAnswer
, or is-correct-answer
, that looks like an easy source of typo bugs as is)