If I have the following event handler bound to my form:
form.addEventListener('submit', function(evt) {
evt.preventDefault();
console.log('submitted');
this.submit();
});
When the form is submitted by the client, the event handler is executed and the form is submitted without the event handler being executed again.
However, the same logic doesn't apply when attaching click handlers to anchor elements:
a.addEventListener('click', function(evt) {
evt.preventDefault();
console.log('clicked');
this.click();
});
The event handler is executed twice and the link is never followed.
I have two questions:
- Why does it appear to work differently for
submitvsclick? - Why does the anchor's
clickhandler only get executed twice and not indefinitely, if the event handler is being executed each time it's called?
this.click()evt.preventDefault(). Here's an example where it works: stackblitz.com/edit/web-platform-c55t7j?file=index.htmladdEventListener('submit')? Unless you meanform.onsubmit = ...or<form onsubmit="...", but the question usesaddEventListener(and my guess is they're all the same in this case)