AddEventListener ошибка при вызове из класса | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

AddEventListener ошибка при вызове из класса

Не понимаю почему выскакивает ошибка в JS при вызове функции. uncaught typeerror: cannot read properties of null (reading 'addeventlistener')

6th Aug 2022, 11:55 PM
Илья
Илья - avatar
2 Answers
+ 1
/* Create Abb object inside a "load" event handler. Scripts are pushed into <head> block when running in SoloLearn mobile app, the DOM isn't yet properly initialized when script is executed. querySelector() accepts a selector, so pass "#ddd" (a valid ID selector) rather than just "ddd". Abb::ccc() doesn't need any argument, you can get rid of that, and adjust the call to Abb::ccc() just like this h.ccc(); // no argument passed You can use classList.toggle("iii") instead of manually adding or removing CSS class https://www.w3schools.com/jsref/prop_element_classlist.asp */ window.onload = function() { var h = new Abb('#ddd'); h.ccc(); } P.S. Next time please, attach your code bit link in post Description for better view of the problem. * Added for context clarity (found in OP's profile) https://code.sololearn.com/WXqrmUUNMbc7/?ref=app
7th Aug 2022, 11:21 AM
Ipang
+ 1
Thank you very much, I learned interesting information.
10th Aug 2022, 2:24 PM
Илья
Илья - avatar