0

Today When I switched from ejs to react I faced the error that document.getElementById is null, and when I went to the browser console, I could easily find it!

Here is my script:

document.getElementById("registerForm").addEventListener('submit', e => {
  e.preventDefault();

...
  
}) 

Here Is The Error: Uncaught TypeError: document.getElementById(...) is null

and here is the snapshot of the browser console showing the element:

The Error

The document in the browser

Please Help!

PS: I am a beginner to reactjs.

kamestart
  • 7
  • 1
  • 5
  • 1
    This issue has nothing to do with react, however you're absolutely not supposed to add event listeners like that. With react, you use `
    this.handleForm(e)} ...>` in your Component. https://reactjs.org/tutorial/tutorial.html#making-an-interactive-component
    – Chris G Apr 29 '21 at 08:23
  • Yes, you are correct, but imagining there is a hacker, he can change the data and do anything! right? – kamestart Apr 29 '21 at 08:46
  • ?? What's your point? He can do that either way. You can never rely on client-submitted information, you always need to validate server-side anyway. – Chris G Apr 29 '21 at 08:47
  • OHHHHHHHHHHHHHHHHHHHHHHHHH............................., Sorry forgot about that. :P. THANKS! – kamestart Apr 29 '21 at 08:55

0 Answers0