Skip to content
Advertisement

AJAX infinite preventDefault() on AJAX-responded form (vanilla JS)

I have reviewed several great Questions along the way, so many that I earned my badge for 40 votes in one day. But, nearly all Questions use a non-Vanilla dependency like jQuery or don’t address my specific need. Here are two examples of wonderful, yet to-my-sorrow unrelated, Questions:

I’m surprised this Question has not been asked because I don’t regard myself to be so imaginative. If this Question is already out there, I want to please open a Meta discussion on how to update that Question so searches can find it.

The situation:

This is Vanilla JS, no dependencies.

The script uses preventDefault() to interrupt a formsubmit button identified by id and call AJAX instead. AJAX works and changes content correctly. Then, the problem begins…

This AJAX responds with a replaced div containing the same form that preventDefault() interrupts. But, AJAX doesn’t recognize that updated formsubmit button after the AJAX response JS-alters the div containing that form with the same id.

Workflow:

  1. HTML -> <form id="same"> (success)
  2. <form id="same"> -> AJAX (success, first AJAX call)
  3. AJAX -> <form id="same"> (success, first AJAX response)
  4. <form id="same"> -> AJAX (broken, second AJAX call)

It works the first time only; I need it to work infinitely.

Note, I need to process this specific form. Other elements, including possible other forms, may be included in this div that AJAX changes.

The code:

index.php:

(AJAX adapted from MDN – Sending forms through JavaScript: Using FormData bound to a form element)

JavaScript

ajax_responder.php :

JavaScript

The question

What is the “right” way to get AJAX to keep acting on the same response-contained formsubmit element?

I have considered ways that do not respond with the same form:

  • Having AJAX only respond and change specific, small elements
  • Relocating the form outside of the AJAX-changed content

But both of those seem complicated.

There must be some way to tell AJAX to preventDefault() on a formsubmit button after that button has been updated by AJAX. I just don’t know what that way is.

Or, is there another solution that still involves Vanilla JS?

Advertisement

Answer

You are binding the eventListener to the <form> ajaxForm. Then you replace it (= remove <form> from DOM and add new <form> to the DOM). In that case the eventListener is also removed.

To have an eventListener on the new form, you can either:

• Replace only the content of the <form>. In that case the DOM element isn’t removed and so the eventListener keeps intact (don’t forget to remove the <form> tag in the php response). This is imho the easiest way.

JavaScript

• Bind a new eventListener everytime the <form> is changed:

JavaScript

You can create a function that can be called on load as well in the ajax response

JavaScript

• bind the eventListener once to the container <div>

JavaScript
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement