This is the one aspect of jQuery and well, Javascript, in general, I see newcomers to Javascript and jQuery get caught upon. Heck, I’ve seen veterans get caught up on how to properly cancel and allow events as well as event bubbling.
Nothing.
There is no opposite method of “event.preventDefault()” to understand why you first have to look into what event.preventDefault() does when you call it.
Underneath the hood, the functionality for preventDefault is essentially calling a return false which halts any further execution. If you’re familiar with the old ways of Javascript, it was once in fashion to use return false for cancelling events on things like form submits and buttons using return true (before jQuery was even around).
So, what is the opposite of event.preventDefault()?
As you probably might have already worked out based on the simple explanation above: the opposite of event.preventDefault()
is nothing. You just don’t prevent the event, by default the browser will allow the event if you are not preventing it.
See below for an explanation:
;(function($, window, document, undefined)) { $(function() { // By default deny the submit var allowSubmit = false; $("#someform").on("submit", function(event) { if (!allowSubmit) { event.preventDefault(); // Your code logic in here (maybe form validation or something) // Then you set allowSubmit to true so this code is bypassed allowSubmit = true; } }); }); })(jQuery, window, document);
In the code above you will notice we are checking if allowSubmit is false. This means we will prevent our form from submitting using event.preventDefault and then we will do some validation logic and if we are happy, set allowSubmit to true.
This is really the only effective method of doing the opposite of event.preventDefault()
– you can also try removing events as well which essentially would achieve the same thing.
Hey Dwayn,
I use in my app grails webflow.
I have which by clicking it the user supposed to redirect to another page.
In the webFlow I have
on(“success”) {}.to “congrats”
on(Exception).to “congrats”
}
congrats {
redirect(controller: “connection”, action: “details”)
}
I want to display onClick this button but for some reason the spinner is not show by $(‘.spinner-ctn’).show(); in FireFox and Safari (only in Chrome it does) so I tried with event.preventDefault() and your solution:
var allowSubmit = false;
if(!allowSubmit) {
e.preventDefault()
$(‘.spinner-ctn’).show()
allowSubmit = true;
}
But nothing happen…. the spinner is displayed but I don’t redirect to another page
okk
Works like a charm. Thanks so much!