Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Button type=submit does not set the SubmitEvent submitter to this #1173

Closed
aaltepet opened this issue Dec 4, 2023 · 1 comment
Closed

Button type=submit does not set the SubmitEvent submitter to this #1173

aaltepet opened this issue Dec 4, 2023 · 1 comment
Labels
bug Something isn't working

Comments

@aaltepet
Copy link
Contributor

aaltepet commented Dec 4, 2023

Describe the bug
HTML form submissions that are triggered from a button click do not pass the clicked button as the event.submitter in the SubmitEvent event. Forms with an onSubmit handler are unable to identify the element that triggered the SubmitEvent. The clicked button, if it has a name, will also be submitted in the GET/POST.

One dev pattern in the remix framework is to use a <button type="submit" name="_action" /> to signal to the POST handler which button was clicked (and branch to take action accordingly). This bug causes problems when using testing-library to write unittests for remix applications that rely on this pattern, as the _action isn't submitted with the POST.

To Reproduce
Steps to reproduce the behavior:

  1. Go to '...'
  2. Click on '....'
  3. Scroll down to '....'
  4. See error

Expected behavior
See the Mozilla Developer Network reference for the SubmitEvent.submitter property

Normal browser behavior for button clicks is to supply the button as the event.submitter. This allows event handlers to identify the element that triggered the event event.target. Additionally if the button has a name, it is included in formData.

For dom like (in react):

<button type="submit" name="_action" value="doTheThing">Submit form</button>
</form>

Clicking the button should emit the button in the console.log.

Remix, a react-based web application that uses leverages browser-standard capabilities like html form submissions, uses this technique to have a single endpoint that can perform multiple tasks. This was also a typical pattern back before single page apps took over.

I see the happy-dom HTMLFormElement code supports supply a submitter, but neither HTMLButtonElement nor HTMLInputElement(type="submit") pass this along to it. I'll be submitting a PR to fix this.

Screenshots

A normal form submission clicked by a button sets the event.submitter to be the button:

Screenshot 2023-12-04 at 12 41 46 PM

Device:
n/a

@aaltepet aaltepet added the bug Something isn't working label Dec 4, 2023
aaltepet added a commit to aaltepet/happy-dom that referenced this issue Dec 4, 2023
capricorn86 added a commit to aaltepet/happy-dom that referenced this issue Jan 15, 2024
capricorn86 added a commit that referenced this issue Jan 15, 2024
#1173@patch: HTMLInputElement[type=submit] and HTMLButtonElement set form submitter on click
@capricorn86
Copy link
Owner

Thank you again for your contribution @aaltepet!

Your fix has been released:
https://github.com/capricorn86/happy-dom/releases/tag/v13.1.4

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants