I’m trying to make a a JS forEach loop that is supposed to run after the DOM is fully loaded, but for some reason it won’t run on code but when I put the same loop on console it runs fine.
`window.onload = (event) => {
<code>var checkboxes = document.getElementsByName("plates")
var labelPlate = document.getElementsByName("labelPlate")
checkboxes.forEach((i, index) => {
if (i.checked) {
labelPlate[index].classList.add("active")
} else {
labelPlate[index].classList.remove("active")
}
})
</code>
<code>var checkboxes = document.getElementsByName("plates")
var labelPlate = document.getElementsByName("labelPlate")
checkboxes.forEach((i, index) => {
if (i.checked) {
labelPlate[index].classList.add("active")
} else {
labelPlate[index].classList.remove("active")
}
})
</code>
var checkboxes = document.getElementsByName("plates")
var labelPlate = document.getElementsByName("labelPlate")
checkboxes.forEach((i, index) => {
if (i.checked) {
labelPlate[index].classList.add("active")
} else {
labelPlate[index].classList.remove("active")
}
})
};`
Loop is supposed to run.
New contributor
Victor França is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.