I'm trying to execute a function after the page load, instead of waiting for an event, in JS. However, I need this function to work with the this keyword. It works perfectly fine with the event, but if I try to load it alone, it displays an error in the console.
function doSomething() {
console.log(this.checked);
}
var myCheckboxes = document.querySelectorAll("input[type='checkbox']");
for (let i = 0; i < myCheckboxes.length; i++) {
myCheckboxes[i].doSomething(); // doesn't work after loading
myCheckboxes[i].addEventListener("change", doSomething); // works on event
}
Thank you.