i want to create a combination game checkin some elements with checkboxes
i need to create a array while the user check a checkbox and compare with the correct array
i dont know who do that
this is my inputs
<section >
<div >
<input type="checkbox" name="tool" id="SBD710" value="SBD710">
<img draggable="true" src="img-game/WOOD/SBD710.png">
</div>
<div >
<input type="checkbox" name="tool" id="SCS220" value="SCS220">
<img draggable="true" src="img-game/WOOD/SCS220.png">
</div>
<div >
<input type="checkbox" name="tool" id="SB201" value="SB201">
<img draggable="true" src="img-game/WOOD/SB201.png">
</div>
<div >
<input type="checkbox" name="tool" id="SB204" value="SB204">
<img draggable="true" src="img-game/WOOD/SB204.png">
</div>
<div >
<input type="checkbox" name="tool" id=SBG700" value="SBG700">
<img draggable="true" src="img-game/WOOD/SBG700.png">
</div>
</section>
<button id="check" >Construye</button>
if somebody can explain me how to create that array with push method and who make the comparison with the correct combination array
CodePudding user response:
Here's the demo for 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);
};
<input type="checkbox" value="SBG701" onChange="check()" />
<input type="checkbox" value="SBG702" onChange="check()" />
<input type="checkbox" value="SBG703" onChange="check()" />
<input type="checkbox" value="SBG704" onChange="check()" />