Trying to make popup window with form in it. When clicking submit, page is refreshing. How can I prevent it? My e.preventDefault
is not working
modalsubmit.addEventListener('submit', function(e) { e.preventDefault(); modal.classList.toggle('overlay_opened'); profilettl.textContent = modalname.value; profdesc.textContent = modaldesc.value; });
Advertisement
Answer
I assume you’ve added the submit event listener to the button and not the form. Check this out and it should work.
const modalsubmit = document.querySelector('form'); modalsubmit.addEventListener('submit', function(e) { e.preventDefault(); });
<form> <button type="submit">submit</button> </form>