thanks to give me your time.
I actually coding a login page but i can’t write form.addEventListener()
When i console.log(form)
it return null and i don’t know why
For the js side :
const form = document.getElementById("form");
console.log(form)
form.addEventListener('submit', (event) => {
event.preventDefault
console.log(event)
console.log(form)
})
For the html side :
[...]
<form id="form">
<label for="email"> Entrez votre Email : </label>
<input id="email" type="email" name="email" placeholder="Email ..." required>
<label for="mdp"> Entrez votre Mot de Passe : </label>
<input type="password" name="password" id="mdp" placeholder="Mot De Passe ..." required>
<input type="submit" name="envoyer" id="send" value="Se connecter">
</form>
[...]
thanks you if you read that !
I tried to change form, re try in another folder but nothing change
3
Answers
preventDefault is a method of the Event object, and you need parentheses on the call to it.
—> event.preventDefault();
It looks like you’re encountering an issue where the form element is returning null in your JavaScript code. This typically happens when the script runs before the HTML document is fully loaded.
To ensure that your script runs after the HTML document has loaded, you can wrap your JavaScript code in an event listener for the ‘DOMContentLoaded’ event. This event ensures that your script executes only after the HTML document has been fully parsed, and the form element is available for manipulation.
Here’s an example:
javascript
This should help resolve the issue of the form element returning null. Additionally, ensure that there are no typos in the id attribute of the form and that there’s no other script interfering with the DOM before your script runs.
Missing the parentheses after
preventDefault
function, you need to correct it like thisevent.preventDefault()
check the next snippet