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.

Click events are triggered with 'enter' keyPress events inside forms

See original GitHub issue

Triggering a keypress enter event inside a form input type=text that has also has a button with a click listener without a defined type will result in a SyntheticMouseEvent click on that button and hit the clickHandler. Adding a type will prevent this behavior.

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Reactions:27
  • Comments:11 (1 by maintainers)

github_iconTop GitHub Comments

70reactions
sophiebitscommented, May 31, 2015

buttons are type="submit" by default in HTML, and pressing enter simulates a click on the button. This is standard HTML behavior, not specific to React.

15reactions
SavePointSamcommented, Feb 7, 2018

It’s actually on the event. Not the target. The following follows the correct pattern to descern the difference between a click event and a return event on a button.

// src/components/Button/Button.js

import React from 'react';

const handleClick = (event) => {
  if (event.detail === 0) {
    // a return event occured
  }

  // some type of click event occured
};

const Button = (props) => (
  <button onClick={handleClick}>{props.label + ''}</button>
);

export default Button;
Read more comments on GitHub >

github_iconTop Results From Across the Web

Trigger a button click with JavaScript on the Enter key in a text ...
I've tweaked your code adding event.preventDefault(); before invoking click(); function while my page has a form and I've switched keyup with keypress as...
Read more >
How To Trigger Button Click on Enter - W3Schools
Trigger a Button Click on Enter. Press the "Enter" key inside the input field to trigger the button: Button. Example.
Read more >
Handling Events :: Eloquent JavaScript
Pressing a key fires "keydown" and "keyup" events. Pressing a mouse button fires "mousedown" , "mouseup" , and "click" events. Moving the mouse...
Read more >
Element: keydown event - Web APIs | MDN
The keydown and keyup events provide a code indicating which key is pressed, while keypress indicates which character was entered. For example, ...
Read more >
Keyboard: keydown and keyup - The Modern JavaScript Tutorial
The keydown events happens when a key is pressed down, and then keyup – when it's released. event.code and event.key. The key property...
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