Skip to main content

Command Palette

Search for a command to run...

React | Should onSubmit Go on the <form> or the <button>?

Updated
1 min readView as Markdown
V

Front-end developer with a background in Korean language QA and education.
Experienced in localisation testing, cross-functional teamwork, and user-focused development.
I enjoy crafting intuitive web experiences with React, Next.js, and TypeScript — always learning, always building.

1. Using onSubmit on the

  • Put onSubmit on the <form>.
  • Use type="submit" on the button.
  • Call e.preventDefault() in your handler if needed.
<form onSubmit={handleSubmit}>
  <input type="text" />
  <button type="submit">Send</button>
</form>

This approach handles submission for the entire form, including when the user presses the Enter key, which improves keyboard accessibility.

2. Using onClick on the

  • Works, but not ideal for forms.
  • Still use type="submit" for accessibility.
<form>
 <input type="text" />
 <button type="submit" onClick={handleSubmit}>Send</button>
</form>

This approach only responds to mouse clicks or taps on the button itself, and may not handle keyboard submission (e.g. pressing Enter) properly, which can negatively affect accessibility.

More from this blog

Valencia Log.dev

9 posts

Hello, I’m Valencia — a self-taught frontend developer based in the UK. This blog is where I share what I’m learning, building, and occasionally messing up. Simple notes, honest progress.