Revised Question
How can I fix the error "Cannot read property 'addEventListener' of null" in my JavaScript code?
I have the following code for adding an event listener to a form on my website:
window.addEventListener('load', function (event) {
const form = document.querySelector('form[data-form-id="xdf-1a43-4db-9d9a-7cgfv61282"]');
console.log(form); // null
form.addEventListener('submit', (event) => {});
});
However, when I run it, I get the error message "Uncaught TypeError: Cannot read property 'addEventListener' of null" in the console. It seems that the form
variable is null, even though I've confirmed that the selector matches a form on the page.
How can I fix this error and add the event listener to the form successfully?