I could make a hidden form by style.display = 'none'
:
var form = document.createElement("form");
form.method = "POST";
form.action = "//test.local/login.php";
form.style.display = 'none';
I thought instead of hiding I could leave form out of the document DOM, like removing the line:
document.body.appendChild(form);
but then:
form.submit();
does nothing. Why attachment to DOM is required for a form for submit()
to work?
2
Answers
The specification for the method says that it must submit the form which says:
The form can’t navigate if the element is not an a element (it’s a
form
) and is not connected. (which it isn’t because it is its own root as it hasn’t been attached to the document).Essentially the it seems the form has to be connected in some way to the DOM for the event to be valid.
This has been covered in more detail here by @Hikarunomemory
https://stackoverflow.com/a/49994802/3381067