I have a button that is inside of a HTML form, but I want button to submit another form on the page. Both forms have unique IDs. How do I identify which form the button should submit?
I have a button that is inside of a HTML form, but I want button to submit another form on the page. Both forms have unique IDs. How do I identify which form the button should submit?
2
Answers
Reading
<button>
documentation was helpful:TLDR use
form
attribute to reference the target form by its ID, e.g.In this example, clicking "Submit Foo" will submit
#foo
form.In this example, the button with the ID
submitOtherForm
is insideform1
. When clicked, it triggers JavaScript to submitform2
. Note that the type of the button is set tobutton
to prevent it from submittingform1
(the default behavior if the type weresubmit
).