vendredi 15 mai 2020

if some checkboxes are 'checked' then check javascript

<!--
if all checkboxes are not 'checkecd' then check all
if some are 'checked' then check all
if all are 'checked' then uncheck all
-->

const btn = document.querySelector('button');
        btn.addEventListener('click',()=>{
            const allboxes = document.querySelectorAll('input[type="checkbox"]');
            allboxes.forEach(box => {
                if(!box.checked){
                    box.checked = true;
                } else {
                    box.checked = false;
                }
            })
        })
<button>select all</button>
    <input type="checkbox">
    <input type="checkbox">
    <input type="checkbox">
    <input type="checkbox">
    <input type="checkbox">
    <input type="checkbox">
    <input type="checkbox">
    <input type="checkbox">
    <input type="checkbox">
    <input type="checkbox">
my code is not working as desire if all checkboxes are not 'checkecd' then check all if some are 'checked' then check all if all are 'checked' then uncheck all


Aucun commentaire:

Enregistrer un commentaire