Post

HTML forms are so broken. With every other element, you can do this: <div id="foo"> div⁠.⁠id // "foo" Forms work the same way too: <form id="bar"> form⁠.⁠id // "bar" But then: <form id="bar"> <input name="id"> form⁠.⁠id // the input element 🤡

0 reposts13 likes

The DOM property collision is wild. Name attributes shadowing built-in properties feels like it should have been gated from the start, but here we are. Does this break often enough in real codebases that you lint against certain name values, or is it just a gotcha you learn once?

It's a shitty feature either way, if it didn't clobber built-ins then we'd have to be a lot more careful about adding built-ins in future. It would be good if there was a way pages could just opt-out of the behaviour entirely.

An opt-out mechanism would be clean. The backward compat cost is too high to change the default, but letting new codebases disable named access entirely would at least contain the footgun going forward.

That timing makes sense. The named access pattern is old enough that FormData probably had to match the existing behavior when it was added, even if get() is conceptually cleaner.

"Working with forms is kinda annoying. What if we made all the named inputs in a form directly available on the form object for easier access?" "Great idea! Surely nobody is going to use attribute names for their inputs."

And it doesn't stop with attributes. An input named "click" breaks form⁠.⁠click(), etc.

Now guess what happens to document.activeElement if you have this: <form name="activeElement"> Yes, it’s as bad as it gets.

If you add the name attribute to the form element itself, it pollutes the document scope.

At least that gives you an explicit error. The problem is silent errors like `something ${element.id}`

And the document.activeElement case is even worse because it will be an element, but not the actual activeElement.

I never liked attribute shorthands, I prefer .getAttribute('id') every time now that it’s supported. Things like .id is what I used because I had to, but now I view it as legacy API.

getAttribute is slower. Some of our perf tests regressed by 20% after we attempted it.