Home > Back-end >  How do I make my page stop refreshing after clicking submit button?
How do I make my page stop refreshing after clicking submit button?

Time:09-18

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;
});

CodePudding user response:

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>

CodePudding user response:

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

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

  • Related