# Never forget type="button" on generated buttons!

DevFeed: [Never forget type="button" on generated buttons!](<https://devfeed.tech/articles/never-forget-type-button-on-generated-buttons-52105.md>)

Original publisher: [Read original article](<https://lea.verou.me/2018/05/never-forget-typebutton-on-generated-buttons/>)

Author: Lea Verou

Published: 2018-05-19T00:00:00Z

Content type: article

Language: en

Sources: [Lea Verou's blog](<https://devfeed.tech/sources/lea-verou-s-blog.md>)

Topics: [selectors](<https://devfeed.tech/topics/selectors.md>), [debugging](<https://devfeed.tech/topics/debugging.md>), [User Interfaces](<https://devfeed.tech/topics/user-interfaces.md>), [Tool](<https://devfeed.tech/topics/tool.md>)

Tags: [app](<https://devfeed.tech/tags/app.md>), [blog](<https://devfeed.tech/tags/blog.md>), [bug](<https://devfeed.tech/tags/bug.md>), [bugs](<https://devfeed.tech/tags/bugs.md>), [button](<https://devfeed.tech/tags/button.md>), [buttons](<https://devfeed.tech/tags/buttons.md>), [code](<https://devfeed.tech/tags/code.md>), [collection](<https://devfeed.tech/tags/collection.md>), [css](<https://devfeed.tech/tags/css.md>), [css-secrets](<https://devfeed.tech/tags/css-secrets.md>), [delete](<https://devfeed.tech/tags/delete.md>), [element](<https://devfeed.tech/tags/element.md>), [elements](<https://devfeed.tech/tags/elements.md>), [event](<https://devfeed.tech/tags/event.md>), [events](<https://devfeed.tech/tags/events.md>), [execution](<https://devfeed.tech/tags/execution.md>), [field](<https://devfeed.tech/tags/field.md>), [form](<https://devfeed.tech/tags/form.md>), [html](<https://devfeed.tech/tags/html.md>), [js](<https://devfeed.tech/tags/js.md>), [keyboard](<https://devfeed.tech/tags/keyboard.md>), [personal](<https://devfeed.tech/tags/personal.md>), [type](<https://devfeed.tech/tags/type.md>), [wtf](<https://devfeed.tech/tags/wtf.md>)

## AI overview

In the described Mavo app, pressing Enter in a collection field triggered deletion because generated `<button>` elements without a `type` default to submit buttons. Inside a form, the first such button became the default submit button; submitting with Enter fires a click on it even when the form's submit event is canceled. The author traced the unexpected deletion to that click and fixed it by adding `type="button"` to buttons generated by Mavo, recommending the same for library-generated buttons used in unfamiliar HTML. The supplied text is an excerpt.

## Source excerpt

I just dealt with one of the weirdest bugs and thought you may find it amusing too. In one of my slides for my upcoming talk "Even More CSS Secrets", I had a Mavo app on a <form>, and the app included a collection to quickly create a UI to manage pairs of values for something I wanted to calculate in one of my live demos. A Mavo collection is a repeatable HTML element with affordances to add items, delete items, move items etc. Many of these affordances are implemented via <button> elements generated by Mavo. Normally, hitting Enter inside a text field within a collection adds a new item, as one would expect. However, I noticed that when I hit Enter inside any item, not only no item was added, but an item was being deleted, with the usual "Item deleted [Undo]" UI and everything! At first I thought it was a bug with the part of Mavo code that adds items on Enter and deletes empty items on backspace, so I commented that out. Nope, still happening. I was already very puzzled, since I couldn't remember any other part of the codebase that deletes items in response to keyboard events. So, I added breakpoints on the delete(item) method of Mavo.Collection to inspect the call stack and see how execution got there. Turned out, it got there via a normal ...click event on the actual delete button! What fresh hell was this? I never clicked any delete button! And then it dawned on me: <button> elements with no type attribute set are submit buttons by default! Quote from spec: The missing value default and invalid value default are the Submit Button state.. This makes no difference in most cases, UNLESS you're inside a form. The delete button of the first item had been turned into the de facto default submit button just because it was the first button in that form and it had no type! I also remembered that regardless of how you submit a form (e.g. by hitting Enter on a single-line text field) it also fires a click event on the default submit button, because people often listen to th