I have an array that starts out empty
let checkboxArray = [];
I also have a table that contains a column of checkboxes with the class eztext
Every time a checkbox is clicked an event handler fires that empties the array and updates it with all of the checkbox.checked values.
const checkboxes = document.querySelectorAll('.eztext');
checkboxes.forEach((checkbox) => {
checkbox.addEventListener('click', function () {
checkboxArray = [];
checkbox.forEach((checkbox) => {
checkboxArray.push(checkbox.checked);
});
});
});
What I’d like to happen now is when a function called init()
is run, I’d like to update all of the checkboxes with the boolean values from the checkboxArray
. However, when I run this function and any variation I’ve tried, they all get checked.
function init() {
const checkboxes = document.querySelectorAll('.eztext');
if (checkboxArray.length > 0) {
checkboxes.forEach((checkbox, i) => {
const isChecked = checkboxArray[i].toString();
checkbox.setAttribute('checked', isChecked);
});
}
}
Thanks for the help!