Skip to content

Bits of .NET

Daily micro-tips for C#, SQL, performance, and scalable backend engineering.

  • Asp.Net Core
  • C#
  • SQL
  • JavaScript
  • CSS
  • About
  • ErcanOPAK.com
  • No Access
  • Privacy Policy
HTML

HTML5: Fix a Form That Submits on Enter From One Input but Not Another

- 03.10.26 - ErcanOPAK

🔧 The Enter Key That Worked in One Box and Not the Next

Pressing Enter inside a text input submits the form it belongs to by default – that’s standard browser behavior with no JavaScript required, built around the form having exactly one implicit submit trigger. The moment a form contains more than one button, or a button of type=”button” placed before the real submit button in the markup, or a textarea instead of a single-line input, that default behavior stops being consistent: Enter inside a plain text input still submits, Enter inside a textarea inserts a newline instead (by design), and Enter inside an input sitting near a non-submit button can end up triggering whichever button the browser’s implicit-submission algorithm picks, which isn’t always the one the form’s author assumed.

🔎 The Problem

<form id="search-form">
  <input type="text" name="query" placeholder="Search...">
  <button type="button" onclick="showFilters()">Filters</button>
  <button type="submit">Search</button>
</form>

<!-- Pressing Enter in the text input: submits the form via the
     "Search" button, as expected, because it's the only button
     of type submit.

     Now add a second real submit-capable element, or switch the
     first button's type by accident during a refactor, and the
     browser's implicit submission rules may pick a different
     control than the one the form's author had in mind - with
     no JavaScript error anywhere, because nothing actually broke;
     the form just submitted "correctly" according to a rule
     nobody checked. -->

✅ Fix: Make the Intended Submit Control Explicit, Not Assumed

  • Keeping exactly one button of type=”submit” in any form, and giving every other button an explicit type=”button”, removes the ambiguity the browser’s implicit-submission algorithm is resolving, since there’s only ever one candidate left for it to pick.
  • For a form that genuinely needs Enter to do something other than the default submit in a specific field (search-as-you-type, a chat message box that should send on Enter but not reload the page), handling the keydown event explicitly and calling preventDefault() on the form’s submit event makes the intended behavior deliberate rather than dependent on markup order.
  • Testing Enter-to-submit behavior specifically after any markup reorder, button type change, or added button – not just testing the mouse-click path – catches a change in implicit submission behavior that a purely visual review of the rendered page would never reveal.

⚠️ Why This Slips Past Code Review

  • The rendered page looks identical whether a button is type=”button” or missing its type attribute entirely (which defaults to type=”submit” inside a form, a detail that surprises a lot of developers) – the visual difference is nothing, but the keyboard behavior difference is total.
  • This is especially common after a refactor that reorders buttons for layout reasons, since the implicit-submission algorithm’s choice can depend on markup order in ways that are specified but rarely memorized, making the resulting behavior change look inexplicable without checking the actual HTML.

A button with no explicit type isn’t a button that does nothing by default – inside a form, it’s a submit button in disguise, and Enter will find it.

— HTML5 Best Practice

Related posts:

HTML Forms Break Accessibility

HTML5 Lazy Loading Images — Zero-Effort Performance Gains

HTML5: Fix a Textarea That Ignores maxlength When Text Is Pasted In

Post Views: 2

Post navigation

Visual Studio: Fix a NuGet Restore That Succeeds on Your Machine but Fails on a Teammate’s
C#: Why Pattern Matching Against a Null Object Doesn’t Throw, and Why That’s Not Always Safe

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

October 2026
M T W T F S S
 1234
567891011
12131415161718
19202122232425
262728293031  
« Sep    

Most Viewed Posts

  • Get the User Name and Domain Name from an Email Address in SQL (973)
  • How to make theater mode the default for Youtube (950)
  • How to add default value for Entity Framework migrations for DateTime and Bool (938)
  • Get the First and Last Word from a String or Sentence in SQL (847)
  • How to select distinct rows in a datatable in C# (836)
  • How to enable, disable and check if Service Broker is enabled on a database in SQL Server (624)
  • Add Constraint to SQL Table to ensure email contains @ (590)
  • Average of all values in a column that are not zero in SQL (553)
  • How to use Map Mode for Vertical Scroll Mode in Visual Studio (526)
  • Find numbers with more than two decimal places in SQL (468)

Recent Posts

  • CSS: Fix a prefers-color-scheme Media Query That Gets Silently Overridden by a Browser Extension’s Forced Dark Mode
  • Git: Fix a Merge Commit That Silently Drops a File Because Both Branches Deleted It Differently
  • HTML5: Fix a Native Lazy-Loading Image That Never Loads Because It Sits Inside a Hidden Tab Until the User Clicks It
  • The AI Prompt That Traces a Null Reference Exception Back to the Exact Line That First Produced the Null
  • The AI Prompt That Turns a Gym Membership Contract’s Fine Print Into a Plain-English List of Cancellation Steps
  • Photoshop: Fix a Color Profile Mismatch That Makes Printed Output Look Nothing Like What You Saw On Screen
  • WordPress: Fix Search Results That Return Pages From a Theme You Deactivated Months Ago
  • Visual Studio: Fix a Test Project That Builds Fine Alone but Fails to Discover Any Tests After a NuGet Restore
  • ASP.NET Core: Fix a File Upload That Times Out on Slow Connections Only Because Kestrel’s Minimum Data Rate Feature Kicked In
  • JavaScript: Fix an Array Destructuring Default Value That Silently Never Applies Because null Was Passed Instead of Undefined

Most Viewed Posts

  • Get the User Name and Domain Name from an Email Address in SQL (973)
  • How to make theater mode the default for Youtube (950)
  • How to add default value for Entity Framework migrations for DateTime and Bool (938)
  • Get the First and Last Word from a String or Sentence in SQL (847)
  • How to select distinct rows in a datatable in C# (836)

Recent Posts

  • CSS: Fix a prefers-color-scheme Media Query That Gets Silently Overridden by a Browser Extension’s Forced Dark Mode
  • Git: Fix a Merge Commit That Silently Drops a File Because Both Branches Deleted It Differently
  • HTML5: Fix a Native Lazy-Loading Image That Never Loads Because It Sits Inside a Hidden Tab Until the User Clicks It
  • The AI Prompt That Traces a Null Reference Exception Back to the Exact Line That First Produced the Null
  • The AI Prompt That Turns a Gym Membership Contract’s Fine Print Into a Plain-English List of Cancellation Steps

Social

  • ErcanOPAK.com
  • GoodReads
  • LetterBoxD
  • Linkedin
  • The Blog
  • Twitter
© 2026 Bits of .NET | Built with Xblog Plus free WordPress theme by wpthemespace.com