javascript is much faster than the dom, thats why you need to let the browser to render the dom first then to exec the js. it is done by this
document.addEventListener("DOMContentLoaded", function (event) {
// content is loaded
const form = document.getElementById("id-of-yow-form");
form.addEventListener("submit", function (event) {
event.preventDefault();
//add yow code
});
});
make sure to add the same id to yow form tag
<form id="id-of-yow-form">
<label for="name">Name: </label>
<input id="name" name="name" type="text">
<label for="city">City: </label>
<input id="city" name="city" type="text">
<button type="submit">submit</button>
</form>
* Be the first to Make Comment