Most active commenters
  • thyristan(5)
  • pverheggen(5)

←back to thread

Just use a button

(gomakethings.com)
285 points moebrowne | 21 comments | | HN request time: 0.45s | source | bottom
1. bugsliker ◴[] No.45774539[source]
- tabindex=0 doesn’t affect ordering, does it?

- why do you need to listen for events at the document level?

not that i disagree with the article, but some arguments didn’t seem right.

replies(2): >>45774646 #>>45774711 #
2. thyristan ◴[] No.45774646[source]
> - tabindex=0 doesn’t affect ordering, does it?

Of course it does. tabindex=0 doesn't sort naturally into the automatic tabindex order, it sorts AFTER everything. So you are jumping through all the other tabindex elements, then you are jumping back to all tabindex=0.

replies(4): >>45774767 #>>45774845 #>>45775649 #>>45777072 #
3. cferdinandi ◴[] No.45774711[source]
Hey, it's me, the original author!

The issue isn't with tabindex=0 specifically, but fucking with tabindex in general. People go down that path, and start putting that shit on everything, like it's Frank's Red Hot.

And in my experience, the same folks who use div's instead of button's are the ones who don't know better and start throwing tabindex around.

"why do you need to listen for events at the document level?"

Not events generally, keydown events specifically, which do not fire on child elements of the document.

replies(3): >>45774861 #>>45775151 #>>45776706 #
4. brandonhorst ◴[] No.45774767[source]
This is incorrect. https://developer.mozilla.org/en-US/docs/Web/HTML/Reference/...
replies(2): >>45775062 #>>45775138 #
5. pverheggen ◴[] No.45774845[source]
That's the same behavior as a <button> without tabindex, like the author is proposing.

It's generally advised not to set tabindex to anything but 0 or -1 and let the document order dictate tab order.

replies(1): >>45775053 #
6. pverheggen ◴[] No.45774861[source]
Not sure about that, MDN's example shows keydown being attached to an element.

https://developer.mozilla.org/en-US/docs/Web/API/Element/key...

7. thyristan ◴[] No.45775053{3}[source]
> That's the same behavior as a <button> without tabindex, like the author is proposing.

Yes, but often you have elements with taborder > 0.

> It's generally advised not to set tabindex to anything but 0 or -1 and let the document order dictate tab order.

Only if document order is sane. Usually with modern websites it isn't, document order is a broken notion if you can position elements at will and e.g. put navigation at the bottom of a document but move it to the top by CSS. Which is actually a recommendation that some people make for acessibility...

What you usually want to do is assign a sensible taborder > 0 to the one form element that the user is probably currently using. Otherwise, he will pointlessly tab through search, menus, cookie bars and a ton of other pointless stuff first.

replies(2): >>45775333 #>>45775968 #
8. thyristan ◴[] No.45775062{3}[source]
That is correct. From your link: "tabindex="0" means that the element should be focusable in sequential keyboard navigation, after any positive tabindex values. The focus navigation order of these elements is defined by their order in the document source. "
9. Ma8ee ◴[] No.45775138{3}[source]
Your link actually supports the comment you replied to.
10. susam ◴[] No.45775151[source]
> Not events generally, keydown events specifically, which do not fire on child elements of the document.

Are you sure? I have a 17 year old HTML tool written using plain, vanilla JavaScript where keydown on a child element seems to have been working as expected.

https://susam.net/quickqwerty.html

https://github.com/susam/quickqwerty/blob/1.2.0/quickqwerty....

Nice article, by the way!

replies(1): >>45775223 #
11. skrebbel ◴[] No.45775223{3}[source]
I think that’s because it’s an input and not a div, so it can get focus. Im not sure whether tabindex is enough to make a div do that too, article suggests no
12. pverheggen ◴[] No.45775333{4}[source]
> Yes, but often you have elements with taborder > 0.

You can just as easily apply the same tabindex to a div though.

> Only if document order is sane. Usually with modern websites it isn't...

Well that's the real problem, all your non-interactive content (like text) is going to be out of order too. You're just adding to the confusion if buttons and other inputs are in a different order from the content they're associated with.

> Otherwise, he will pointlessly tab through search, menus, cookie bars and a ton of other pointless stuff first.

The proper way of dealing with this is a "Skip to Main Content" element:

https://webaim.org/techniques/skipnav/

replies(1): >>45775653 #
13. bugsliker ◴[] No.45775649[source]
I'm saying tabindex=0 is naturally sorted wrt other naturally focusable elements. That matches the behavior of the <button> you're trying to emulate. I don't know what tabindex>0 has to do with this.

See this fiddle https://jsfiddle.net/483uqjnp/

(again, I do not condone building your own <button>, just pointing this out)

14. thyristan ◴[] No.45775653{5}[source]
> The proper way of dealing with this is a "Skip to Main Content" element: > > https://webaim.org/techniques/skipnav/

No, it isn't the proper way. That only works if you can see the skip link and know to press enter. Otherwise you will tab straight into the navigation. So possibly useful for screen readers, but completely useless for most keyboard users. Yet another stupid webdev workaround for a selfimposed problem.

What you should do is autofocus the first form element (if there is a form), give it tabindex=1 and number the other form elements in a sensible ascending tabindex order. Otherwise, proper semantic markup is sufficient, even for screen readers.

replies(1): >>45776080 #
15. MrJohz ◴[] No.45775968{4}[source]
You almost certainly don't want to assign tab order manually. Theoretically, if your html is out-of-order for some reason, then you can fix it with taborder, but there are so many issues with out-of-order html that it's much better to avoid that in the first place. Even on modern websites, it is almost always easier to lay your html out in the correct order rather than messing around with the order via CSS. (I read an article recently by a web accessibility expert that discussed the issue of flexbox's order parameter and how the working group designing it failed to accessibility into account when adding it to the spec: https://alice.boxhall.au/articles/a-threat-model-for-accessi...)

You mention using it to skip a nav header or something similar, but (1) you have autofocus for that if you need it (and you don't usually need it), and (2) the pattern where you provide a "jump to content" button is typically a better approach, because it allows the user to decide what they want to do when they arrive on your site, rather than you deciding for them. If the "jump to content" button behind visible when focused and is the first element on the screen, then it works perfectly for screen readers and other keyboard users, and you don't need to handle taborder manually.

There are always exceptions to these sorts of rules, and some times tabindex might be necessary, but I've not yet come across a case where the problem couldn't be solved better in a different way.

16. pverheggen ◴[] No.45776080{6}[source]
Usually you would have it appear when it's focused, like this for example:

https://www.nytimes.com/

And yes, this is an acceptable solution according to the W3C:

https://www.w3.org/WAI/WCAG21/Techniques/general/G1

Your solution of focusing the first form element is pretty idiosyncratic. It's better to follow WAI patterns, because patterns have predictable behavior. Otherwise, keyboard users will have to learn how to interact with your website from scratch, instead of just following the same pattern they're used to from other sites.

replies(1): >>45776166 #
17. thyristan ◴[] No.45776166{7}[source]
Any website implementing a form for data entry is expected by any sane user to autofocus the first form element.
replies(2): >>45776538 #>>45777138 #
18. pverheggen ◴[] No.45776538{8}[source]
Sorry, missed the part about "form for data entry". If that's the main point of the given page, then sure, focus on the first input is fine.

Your original comment was to use tabindex to skip search, menu bars, breadcrumbs, etc. and for that there are better options.

19. kyle-rb ◴[] No.45776706[source]
Hi, good premise overall, but there are just a lot of little things that are off.

- It only counts as "fucking with tabindex" if you give it a value that's not 0 or -1. You should give that specific disclaimer, because there are uses for tabindex=0 other than reimplementing <button>.

- Divs can definitely receive keydown events. If I go to an arbitrary web page, pick a div and run `div.tabIndex = 0;` + `div.addEventListener('keydown', console.log);`, I see those events coming through when I have the div keyboard-focused.

- "Run your code, somehow..." I think just calling `notRealBtn.click()` is the best option.

- Stupid but semi-interesting nitpick: 'keydown' is good for enter, but you should be listening to 'keyup' for the space bar. That's how real <button>s work anyway.

- The 'keyup' listener should call event.preventDefault() to prevent the default behavior of the space bar scrolling the page.

20. minitech ◴[] No.45777072[source]
tabindex=0 does sort naturally into the automatic tabindex order.

> So you are jumping through all the other tabindex elements

This part is correct (for elements with an explicit positive tabindex), which is why specifying an explicit positive tabindex is considered a code smell. If you don’t specify a tabindex on an element that’s focusable by default, it behaves like tabindex=0.

Try it:

  data:text/html,<button>foo</button><i tabindex=0>bar</i><button>baz</button>
21. MrJohz ◴[] No.45777138{8}[source]
I would be very surprised if a website autofocused almost anything on page load, except for very specific applications where that makes sense. I just tried clicking through some forms on gov.uk, which is a website that has spent a _lot_ of time testing and improving the UX of their forms, and none of them had autofocused elements, not even on later pages after having already filled in elements.

I can imagine screen readers would deal particularly poorly with this behaviour, because the user would be dropped in a field without any of the context of the page, just the field label to work with. However, I've not been able to test that out properly.

I don't think the behaviour you're describing is anywhere near as common as you think it is, and I suspect it would make a page less accessible for a number of kinds of users.