Hey Everyone and thank you for your time. I have a problem to submit a form in JS. Obviously my form has the value “null”, but i don’t can figure it out why. Can you help me, im new to JS.
HTML:
<form id="form">
<input type="text" id="username" placeholder="Username" required>
<input type="email" id="email" placeholder="Email" required>
<input type="submit" value="Register">
</form>
JS:
var form = document.getElementById('form')
form.addEventListener('submit',function(event){
event.preventDefault()
var username = document.getElementById("username").value
console.log(username)
var email = document.getElementById("email").value
console.log(email)
})
Error Massage:
Uncaught TypeError: Cannot read properties of null (reading ‘addEventListener’)