I am trying to add a certain string "classAtoImage" to an empty array. Here is my code:
const tomato = document.getElementById('inputA');
var testArr = [];
tomato.addEventListener('change', (e) => {
if (e.target.checked || !(tomato.classList.contains('classAtoImage'))) {
testArr.push('classAtoImage');
console.log(testArr);
} else {
testArr.pop('classAtoImage');
console.log(testArr);
}
});
But in the output array, it keeps adding the same string every time I click on the checkbox.
Output: ["classAtoImage", "classAtoImage", "classAtoImage"]
I am expecting to add if the conditions true or remove if it is false. NOTE: I am listening for a 'change' event on the check box.
Aucun commentaire:
Enregistrer un commentaire