0

I've spent hours and read countless stackoverflows but I can't get this to work.

I've got a form (Caldera Form in Wordpress) that has a submit button (in fact it's an input tag). Via JS I added an event listener because I want to make a backup of the form data to my database. The event listener line:

document.querySelector(".submit-permanently").addEventListener("click", handleIorSaveClick);

The handleIorSaveClick function does the job pretty well and everything is fine so far. However it's not submitting the form now any longer. Although I didn't include a "prevent default" statement anywhere. It just calls my handleIorSaveClick function (which does the database magic) and nothing else. The form isn't getting submitted any longer.

Because of this I had the idea to place a fake button that looks like the submit button while making the real submit button invisible. If the user clicks the fake button, the event listener is calling the handleIorSaveClick function and from within there I want to automatically click on the "real" submit button after the database magic is done.

To do this I included these three lines of code:

document.querySelectorAll('.real-submit')[0].style.visibility = "visible";
document.querySelectorAll('.real-submit')[0].style.display = "inline";
document.querySelectorAll('.real-submit')[0].click();

Oddly enough the first two lines are being executed: The button becomes visible. But the third line which (in my head) should simulate a user click on that button to actually submit the form won't happen. I could even select the button in the chrome dev console with document.querySelectorAll('.real-submit')[0] but this is giving me an undefined error: document.querySelectorAll('.real-submit')[0].click();

Can anyone help me either telling me why the .click() doesn't do anything or telling me why the event listener is preventing the form from being submitted in the first place?

Tarik A.
  • 3
  • 2
  • 2
    You'll need to provide the relevant HTML and CSS for us to really understand what's going on. Also, [`document.querySelectorAll('.real-submit')[0]` is pretty inefficient code](https://stackoverflow.com/questions/54952088/how-to-modify-style-to-html-elements-styled-externally-with-css-using-js/54952474#54952474) that should be replaced with `document.querySelector(".real-submit")`. – Scott Marcus Jan 09 '21 at 02:23
  • I used a mix of your comment and Vahids answer by selecting my form and submitting it this way: document.querySelector(".caldera_forms_form").submit(); So a big thanks to you as well! – Tarik A. Jan 09 '21 at 13:06

1 Answers1

1

You can do two things, In your first approach, without making fake submit button, at the end of handleIorSaveClick() function, unbind event listener from '.submit-permanently' button and then call

document.querySelectorAll('.real-submit')[0].click();

Or in your second scenario, instead of

document.querySelectorAll('.real-submit')[0].click();

just do as this:

document.getElementById("myForm").submit();
Vahid
  • 655
  • 4
  • 11