Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Firefox won't submit a form created by JavaScript

I need to create a form with a few inputs when an event happens. My code is below.

Chrome submits fine - the alert box shows and the page changes.

Firefox does not work - the alert box shows but the page stays the same. How can I get Firefox to submit the form?

var idsInput = document.createElement('input');
idsInput.name = 'itemIds';
idsInput.value = ids;

var quantityInput = document.createElement('input');;
quantityInput.name = 'quantity';
quantityInput.value = 1;

var authTokenInput = document.createElement('input');
authTokenInput.name = 'authenticityToken';
authTokenInput.value = '${session.getAuthenticityToken()}';

var submitInput = document.createElement('input');
submitInput.type = 'submit';
submitInput.value = 'anything';

var form = document.createElement('form');;
form.action = '@{Checkout.setItemsQuantityHandler}';
form.method = 'POST';
form.elements[0] = idsInput;
form.elements[1] = quantityInput;
form.elements[2] = authTokenInput;
form.elements[3] = submitInput;
form.submit();

alert('after submit()'); // for debugging only
like image 771
Amy B Avatar asked Mar 06 '11 02:03

Amy B


People also ask

Why submit button is not working in JavaScript?

Sometimes the problem is caused by old versions of the Javascript files, cached by your browser and can be fixed by clearing the browser cache. You can use the browser console of your browser for debugging. After the Javascript error is fixed, the submit button will automatically be enabled.

How do you submit a form using JavaScript?

In the body tag, created an HTML form and specify the id, method, and action of the form. In the form, specify an anchor tag with an event onclick. Create a function for JavaScript that will get executed when the link is clicked. When we click on the link, the function submitForm() will get executed.

What does JavaScript submit () do?

submit() allows to initiate form sending from JavaScript. We can use it to dynamically create and send our own forms to server.


1 Answers

FF requires it to be in the DOM already. Set form to display:none and add it to an existing element in DOM and then submit it.

like image 56
BalusC Avatar answered Sep 28 '22 03:09

BalusC