dimanche 27 mars 2022

Javascript Checkbox.checked to select all ,cannot be deselect [duplicate]

<script>
       let SelectAll = document.querySelector('input')
        let items = document.querySelectorAll('input:nth-child(n+2)')
        // let a1 = SelectAll.checked
        // console.log(a1)
        

        SelectAll.onclick = function () {
            let SelectAll_status = SelectAll.checked
            console.log(SelectAll_status)
            // console.log(items.length)
            if (a1 = true) { 
          for (var i = 0;i < items.length;i++) {
            items[i].setAttribute("checked", "true");
        
          } } else {
            for (var j = 0;j < items.length;j++) {
            items[j].setAttribute("checked", "false");
            let return1 = items[j].checked
            
          }
          console.log (return1)
        }}
</script>

I use the If condition. But when I deselect the tick. Nothing works. Even the whole part of "Else" doesn't work.

At beginning, I used "items[i].checked= SelectAll.checked " , at console.log , "false" is given to items, but the tick toggle doesn't show. So,I need to use setAttribute. Then I need to use If conditions. And problems appear...




Aucun commentaire:

Enregistrer un commentaire