Uncaught Type error: Cannot read property 'addEventListener' of null | Sololearn: Learn to code for FREE!
Novo curso! Todo programador deveria aprender IA generativa!
Experimente uma aula grƔtis
+ 1

Uncaught Type error: Cannot read property 'addEventListener' of null

Please I need help, I don't understand what that mean my code : var para = document.querySelector('p) ; var rougit = function(){ Para. classlist.toogle('red'); } ; para.addEventListener('click', rougit);

2nd Feb 2019, 9:20 AM
Alexis
Alexis - avatar
8 Respostas
+ 3
you missed the closing quote in the query Selector, declared variable is para but you're using Para in the function Can you share the complete code? If you're running this in sololearn code playground you need to wrap your js in a function that loads after the dom is loaded: window.onload = function() { //Your code here };
2nd Feb 2019, 9:50 AM
Random
Random - avatar
+ 4
Ankit thank you very much ;)
2nd Feb 2019, 12:57 PM
Alexis
Alexis - avatar
+ 3
Also it should be classList and toggle instead of classlist and toogle
2nd Feb 2019, 9:55 AM
Random
Random - avatar
+ 2
The program basically correct, but there were many typing mistakes you made. As a programmer, you should type in precise way, it would save you a lot of time used for debugging. <!DOCTYPE html> <html> <head> <title>QnA</title> <style> .red { color: red; } </style> </head> <body> <p>Click here</p> <script> var para = document.querySelector('p'); var rougit = function(){ para.classList.toggle('red'); }; para.addEventListener('click', rougit); </script> </body> </html> https://code.sololearn.com/WSBoEG5W4Akm/?ref=app
2nd Feb 2019, 10:44 AM
CalviÕ²
CalviÕ² - avatar
+ 2
CalviÕ² I do not see my fault?
2nd Feb 2019, 12:47 PM
Alexis
Alexis - avatar
+ 2
OK, Thanh you CalviÕ²
2nd Feb 2019, 2:56 PM
Alexis
Alexis - avatar
0
In JS pane, you need to put your code on onload callback. https://code.sololearn.com/WLFNbeUR0Yir/?ref=app
2nd Feb 2019, 2:51 PM
CalviÕ²
CalviÕ² - avatar