Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery - prevent default, then continue default

I have a form that, when submitted, I need to do some additional processing before it should submit the form. I can prevent default form submission behavior, then do my additional processing (it's basically calling Google Maps API and adding a few hidden fields to the form) -- and then I need the form to submit.

Is there a way to "prevent default", then some point later "continue default?"

like image 987
StackOverflowNewbie Avatar asked Sep 27 '22 13:09

StackOverflowNewbie


People also ask

How do I submit after preventDefault?

submit(function(e){ e. preventDefault(); // Cycle through each Attendee Name $('[name="atendeename[]"]', this). each(function(index, el){ // If there is a value if ($(el). val()) { // Find adjacent entree input var entree = $(el).

What is the opposite of event preventDefault ()?

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.

What is prevent default in jquery?

preventDefault() method stops the default action of an element from happening. For example: Prevent a submit button from submitting a form. Prevent a link from following the URL.

What is the correct Behaviour of preventDefault () function?

The preventDefault() method cancels the event if it is cancelable, meaning that the default action that belongs to the event will not occur. For example, this can be useful when: Clicking on a "Submit" button, prevent it from submitting a form. Clicking on a link, prevent the link from following the URL.


1 Answers

Use jQuery.one()

Attach a handler to an event for the elements. The handler is executed at most once per element per event type

$('form').one('submit', function(e) {
    e.preventDefault();
    // do your things ...

    // and when you done:
    $(this).submit();
});

The use of one prevent also infinite loop because this custom submit event is detatched after the first submit.

like image 75
Aurel Avatar answered Oct 07 '22 23:10

Aurel