Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to reenable event.preventDefault?

I have a web page which I have prevented the default action on all submit buttons, however I would like to re-enable default submit action on a button how can I do this?

I am currently preventing the default action using the following:

$("form").bind("submit", function(e){ e.preventDefault(); }); 

I have successfully done this using the following:

$(document).ready(function(){ $("form:not('#press')").bind("submit", function(e){ e.preventDefault(); }); 

But can I do this dynamically when the button is clicked?

like image 987
Richbits Avatar asked Jul 22 '09 09:07

Richbits


People also ask

How do I reset event preventDefault?

submit(function(event) { event. preventDefault(); return false; }); } }); function validateField() { var first_name = $('#first_name').

How do I enable preventDefault?

$(document). ready(function(){ $("form:not('#press')"). bind("submit", function(e){ e. preventDefault(); });

What does event preventDefault () do?

preventDefault() The preventDefault() method of the Event interface tells the user agent that if the event does not get explicitly handled, its default action should not be taken as it normally would be.

How can we prevent default behavior in react?

We can prevent this default behaviour by making a small modification to the definition of the handleSubmit function. We call a preventDefault on the event when submitting the form, and this will cancel the default event behavior (browser refresh) while allowing us to execute any code we write inside handleSubmit.


2 Answers

You would have to unbind the event and either rebind to a separate event that does not preventDefault or just call the default event yourself later in the method after unbinding. There is no magical event.cancelled=false;

As requested

 $('form').submit( function(ev){           ev.preventDefault();           //later you decide you want to submit          $(this).unbind('submit').submit()    }); 
like image 139
redsquare Avatar answered Sep 28 '22 22:09

redsquare


Either you do what redsquare proposes with this code:

function preventDefault(e) {     e.preventDefault(); } $("form").bind("submit", preventDefault);  // later, now switching back $("form#foo").unbind("submit", preventDefault); 

Or you assign a form attribute whenever submission is allowed. Something like this:

function preventDefault(e) {     if (event.currentTarget.allowDefault) {         return;     }     e.preventDefault(); } $("form").bind("submit", preventDefault);  // later, now allowing submissions on the form $("form#foo").get(0).allowDefault = true; 
like image 44
Patrice Neff Avatar answered Sep 28 '22 21:09

Patrice Neff