question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

clicking span inside a submit button does not submit the form

See original GitHub issue

Basic info:

  • Node.js version: >=8
  • jsdom version: 13

Minimal reproduction case

Repl.it

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?

jsbin

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:closed
  • Created 5 years ago
  • Reactions:6
  • Comments:8 (1 by maintainers)

github_iconTop GitHub Comments

4reactions
Zirrocommented, Dec 7, 2018

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

2reactions
kentcdoddscommented, Dec 11, 2018

Woo! Thank you 🎉

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found