skip to Main Content

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


  1. The specification for the method says that it must submit the form which says:

    If form cannot navigate, then return.

    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).

    Login or Signup to reply.
  2. 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

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search