-1

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;
});
Fadi Hania
  • 705
  • 4
  • 11
Prains
  • 17
  • 5
  • Can you add more details about HTML? The `modalsubmit` element? Try having a full snippet including HTML and JS – Fadi Hania Sep 17 '22 at 09:48
  • Check out [this](https://stackoverflow.com/questions/3362481/e-preventdefault-not-working) SO question! Try adding `e.stopImmediatePropagation()` [(mdn web docs)](https://developer.mozilla.org/en-US/docs/Web/API/Event/stopImmediatePropagation). – philale Sep 17 '22 at 09:50

2 Answers2

0

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>
Amini
  • 1,620
  • 1
  • 8
  • 26
-1

Use the e.stopPropagation() to interrupt the default behavior

https://developer.mozilla.org/en-US/docs/Web/API/Event/stopPropagation