0

Consider I have the following HTML:

<form id="myForm" action="/echo/html/" method="post">
  <input type="text" value="10" />
  <input type="submit" value="submit" />
</form>
<input type="submit" form="myForm" value="submit" />
<input id="myInput" type="text" form="myForm" />

and with that HTML I have the following JS:

document.addEventListener('submit', function(e) {
  console.log(e, 'addEventListener');
});

document.getElementById('myInput').addEventListener('keyup', function(e) {
  console.log(e.target.form);
  e.target.form.submit();
});

(Note: this code has been optimized to work in jsfiddle and thus uses /echo/html/. The docs on this are here)

There are three parts to the above code:

  1. The <form> (id="myForm"), which has a text input and a submit input.
  2. The submit input outside myForm, but, using the attribute form="myForm" will submit the data inside myForm.
  3. The text input which when registers a keyup, will fire myForm by using e.target.form.submit(). This does not, however, fire the submit event attached to the document.

My question is:

How do I simulate/trigger/etc. a submit event, which actually fires a submit event which is currently being listened by the document, using the 3rd method I describe above (on a keyup for a text input).

P.S. I could not get a snippet working as it was erroring in stackoverflow, so the jsfiddle is here.

3 Answers 3

1

I think this happens because you're triggering the event on the form, without it bubbling to document. I'm sadly unable to find out any links to the documentation about it (as, on why it happens). If anybody is able to provide such link, I'll be interested to read about it as well.

To get around this, I've created a submit Event which actually bubbles, that is captured by document as well:

document.addEventListener('submit', function(e) {
    console.log(e, 'documentEventListener');
});

document.getElementById('myForm').addEventListener('submit', function(e){
    console.log(e, 'myFormEventListener');
})

document.getElementById('myInput').addEventListener('keyup', function(e) {
    var event = new Event("submit", { bubbles: true });
    this.form.dispatchEvent(event);
});

https://jsfiddle.net/c1vnjjxr/3/

Sign up to request clarification or add additional context in comments.

3 Comments

interesting! well this works and does what I need it to do. Weird on .submit() (maybe) no properly bubbling to document.
I think that the explanation here is that programatically triggered events do not bubble (unlike mouse events), but I failed to find reference for that.
Makes sense. It would be cool if .submit() took a bubble param, almost like new Event. I'm guessing something like: this.form.submit({ bubbles: true }); but your way works (just realized you could even one line it like this: this.form.dispatchEvent(new Event("submit", { bubbles: true }));
0

The problem is the "myInput" input field is outside the form tag already. To submit the form using the outside input is to get the form and use the .submit() method.

document.addEventListener('submit', function(e) {
  console.log(e, 'addEventListener');
});

document.getElementById('myInput').addEventListener('keyup', function(e) {
  // console.log(e.target.form); <-- what is this for??
  document.getElementById("myForm").submit();
});

1 Comment

HTML5 allows you for placing input elements outside the form they belong to - that's what form attribute is for. You can access the form by accessing .form property - so, either e.target.form (e.target is target of keyup event, in this case #myInput) or this.form is fine
0

Why would you need this?

document.addEventListener('submit', function(e) {
  console.log(e, 'addEventListener');
});

Just put the inputs inside the form, so they'll be able to submit it.

1 Comment

my idea was to use the form attribute on the text input (<input form="myForm") so that I can place the text input elsewhere on the page but still have it connected to myForm

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.