clicking span inside a submit button does not submit the form
See original GitHub issueBasic info:
- Node.js version: >=8
- jsdom version: 13
Minimal reproduction case
const {JSDOM} = require('jsdom')
const {window, window: {MouseEvent, document}} = new JSDOM(`
<!doctype html>
<html>
<body>
<form>
<button type="submit">
<span>submit</span>
</button>
</form>
</body>
</html>
`)
const form = document.querySelector('form')
const submit = document.querySelector('span')
let calls = 0
form.addEventListener('submit', event => {
event.preventDefault()
console.log('submitted')
calls++
})
const clickEvent = new MouseEvent('click', {bubbles: true, cancelable: true, button: 0})
submit.dispatchEvent(clickEvent)
console.assert(calls === 1, 'the form was not submitted')
How does similar code behave in browsers?
In the browser, firing a click event on the span within a submit button will call the form’s submit handler. In jsdom it does not.
Note that if I select the button instead of the span and fire the click event on the button then the form is submitted just fine. It’s just when I select something within the button.
Real world situation: https://github.com/kentcdodds/react-testing-library/issues/234
Thanks!
Issue Analytics
- State:
- Created 5 years ago
- Reactions:6
- Comments:8 (1 by maintainers)
Top Results From Across the Web
HTML button to NOT submit form - javascript - Stack Overflow
The default behavior is to submit the form, most people don't realize that. The type parameter has three options: submit (default), button and...
Read more >.submit() | jQuery API Documentation
The submit event is sent to an element when the user is attempting to submit a form. It can only be attached to...
Read more >ARIA: button role - Accessibility - MDN Web Docs
A button is a widget used to perform actions such as submitting a form, opening a dialog, canceling an action, or performing a...
Read more >Browser default actions - The Modern JavaScript Tutorial
A click on a form submit button – initiates its submission to the server. ... If we handle an event in JavaScript, we...
Read more >HTML submit button onclick code - HTML Form Guide
In both the cases, pressing the button will submit the parent form without the need for handling the onclick event separately.
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
@kentcdodds Thanks for the detailed report! https://github.com/jsdom/jsdom/pull/2450 should fix it and be ready for merge once I’ve written a new test.
Woo! Thank you 🎉