Rails 6: SimpleFrom Remote: true submit via javascript/Jquery

662 Views Asked by At

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.

1

There are 1 best solutions below

0
On

It may be too late to answer, but I solved same issue with this code.

form = document.querySelector('#form_id');
form.dispatchEvent(new Event('submit', {bubbles: true})); // you can specify more options in `Event()` for reliability across different browsers.

It is from : https://stackoverflow.com/a/52304806/1529453