This content originally appeared on Lea Verou’s blog and was authored by Lea Verou
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 that instead of the form’s submit event. Ironically, I was cancelling the form’s submit event in my code, but it still generated that fake click event, making it even harder to track down as no form submission was actually happening.
The solution was of course to go through every part of the Mavo code that generates buttons and add type=“button” to them. I would recommend this to everyone who is writing libraries that will operate in unfamiliar HTML code. Most of the time a type-less <button>
will work just fine, but when it doesn’t, things get really weird.
This content originally appeared on Lea Verou’s blog and was authored by Lea Verou
![](https://www.radiofree.org/wp-content/plugins/print-app/icon.jpg)
Lea Verou | Sciencx (2018-05-19T00:00:00+00:00) Never forget type="button" on generated buttons!. Retrieved from https://www.scien.cx/2018/05/19/never-forget-typebutton-on-generated-buttons-2/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.