Uncaught TypeError: Cannot read property * of undefined
-
Hello everyone:]
I try to use the next js to add some classes for buttons in the addressfield:
function stylizeAddressfieldButtons(fieldname) { var buttons = document.querySelectorAll('.' + fieldname + ' button.button-addressfield'); buttons[0].classList.add("left-button-addressfield"); // * this line buttons[buttons.length - 1].classList.add("right-button-addressfield"); } stylizeAddressfieldButtons('addressfield'); stylizeAddressfieldButtons('searchfield');
But it doesn't works and shows the error
Uncaught TypeError: Cannot read property 'classList' of undefined
with a refer to the marked line. But if I try this code in the console, it works without any errors. Somehowbuttons
are empty to the start of the script, and I think, there must be some event to run the script only after browser load, but I don't know which. I triedDOMContentLoaded
,window.onload
andbody onload="function()"
, but it doesn't works. I tried to put my script connection to the end of thebody
and to the end of thehead
in browser.html, and it still doesn't works.Does anyone know how to fix it?
-