search

Home  >  Q&A  >  body text

Create array with checkboxes using JavaScript

I want to create a combo game using checkboxes to check in some elements

I need to create an array when the user selects a checkbox and compare it to the correct array

I don’t know who did it

This is my input

<section class="draggable-items">
    <div class="check-tool">
        <input type="checkbox"  class="checkbox-tool" name="tool" id="SBD710" value="SBD710">
        <img class="draggable correct" draggable="true" src="img-game/WOOD/SBD710.png">
    </div>
    <div class="check-tool">
        <input type="checkbox" class="checkbox-tool" name="tool"  id="SCS220" value="SCS220">
        <img class="draggable correct" draggable="true" src="img-game/WOOD/SCS220.png">
    </div>
    <div class="check-tool">
        <input type="checkbox" class="checkbox-tool" name="tool"  id="SB201" value="SB201">
        <img  class="draggable correct" draggable="true" src="img-game/WOOD/SB201.png">
    </div>
    <div class="check-tool">
        <input type="checkbox" class="checkbox-tool" name="tool"  id="SB204" value="SB204">
        <img class="draggable correct" draggable="true" src="img-game/WOOD/SB204.png">
    </div>
    <div class="check-tool">
        <input type="checkbox" class="checkbox-tool" name="tool"  id=SBG700" value="SBG700">
        <img  class="draggable correct" draggable="true" src="img-game/WOOD/SBG700.png">
    </div>


</section>
    <button id="check" class="btn">Construye</button>

If someone can explain how to create this array using Push method and who made it Compare with correct combined array

P粉905144514P粉905144514279 days ago316

reply all(1)I'll reply

  • P粉729518806

    P粉7295188062024-02-27 07:32:07

    This is a demo of your solution

    
     let check = function () {
      let checkboxes = document.querySelectorAll("input[type=checkbox]");
      let result = [];
      for (let i = 0; i < checkboxes.length; i++) {
            if (checkboxes[i].checked) {
               result.push(checkboxes[i].value);
            } else {
               result.push('null');
            }
      }
    
      console.log(result);
    };
    
    
    
    

    reply
    0
  • Cancelreply