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
JavaScript
x
7
1
modalsubmit.addEventListener('submit', function(e) {
2
e.preventDefault();
3
modal.classList.toggle('overlay_opened');
4
profilettl.textContent = modalname.value;
5
profdesc.textContent = modaldesc.value;
6
});
7
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.
JavaScript
1
5
1
const modalsubmit = document.querySelector('form');
2
3
modalsubmit.addEventListener('submit', function(e) {
4
e.preventDefault();
5
});
JavaScript
1
3
1
<form>
2
<button type="submit">submit</button>
3
</form>