A JavaScript Curiosity Regarding addEventListener

I’m wondering why this code fails to stop a form submission

document.forms[0].addEventListener("submit",falsify, false);
function falsify() {
		return false;
}

and this code successfully kills it?

document.forms[1].onsubmit = falsify;
function falsify() {
		return false;
}

Try it out (Firefox/Safari/Chrome/Opera only- there’s no addEventListener support in Internet Explorer). The form action on both is an alert. The first executes every time. The second never does.

Anyone out there know enough about the inner workings on addEventListener to explain why those two similar code blocks behave so differently? I’ve poked around a lot of the usual suspects (mdc, ppk, w3c) and haven’t seen anything that speaks to the above behavior, so I’m opening it up to the Internet. Hopefully someone out there can satisfy my curiosity on this matter.

This entry was posted in JavaScript, Web. Bookmark the permalink.

4 Responses to "A JavaScript Curiosity Regarding addEventListener"

Leave a reply