I have the following simple form:
<form action="" id="myForm">
Use <code>return false</code> (if not checked,
<code>e.preventDefault()</code> will be used):
<input type="checkbox" id="myCheckbox" checked>
<br/>
<input type="submit" value="Submit me">
</form>
Then, the JavaScript side:
document.getElementById("myForm").addEventListener("submit", function (e) {
if (document.getElementById("myCheckbox").checked) {
return false;
}
e.preventDefault();
});
Why return false
does NOT prevent the default behavior, while e.preventDefault()
works as supposed?
According to this answer:
return false
is doing 3 separate things when you call it:
event.preventDefault();
event.stopPropagation();
- Stops callback execution and returns immediately when called.
So, event.prevendDefault()
is called. Does this happen in a jQuery callback only?
return false
is handled by browser, not by jQuery. – Dactyl