I have read this submit a rails remote form with javascript
and none of the solutions seem to work. I just need to run a callback before the submit gets called.
View
<form id="formA" " novalidate="novalidate" action="https://www.demo.com/form.php" accept-charset="UTF-8" data-remote="true" method="post" _lpchecked="1">
...
</form>
JS
var form = document.forms.item(0)
form.addEventListener('submit', async function(e) {
e.stopImmediatePropagation()
e.preventDefault();
...
form.submit()
});
My form does not seem to submit with remote: true, so no ajax, I get a html response instead of a js response.
form.addEventListener("ajax:before", function (event) => { ... })"ajax:before"event does not fire. Since a remote check is made beforehand.remote: false, for that you'll have to make the remote check yourself. Eg.if (e.target.hasAttribute("data-remote") && e.target.getAttribute("data-remote") != "false") { Rails.handleRemote.call(e.target, e) } else { form.submit() }You might also want to disable the submit-button while your async stuff is being processed, to prevent the user from sending multiple "submit" events.