18
loading...
This website collects cookies to deliver better user experience
div
as a button pattern is by far the most common. Here's a code snippet to demonstrate it:<div onClick="...">
I pretend to be a button
</div>
tabindex="0"
to make them focusable with a keyboard. But they never, ever seem to have the keyboard event listeners for space and enter. This means that even if the user can focus on the so-called button, they can't activate it with a keyboard.div
work as a button for all these groups, but it's lots of work and makes the code harder to maintain. And you know, by using the button element, you'd get all that for free.<a href="...">
I, too, pretend to be a button
</a>
<a role="button" tabindex="0" onClick={…}>
I'm a link, I'm a button. Who am I?
</a>
href
-attribute. Removing the href
-attribute strips away the semantics and keyboard interaction. This way, even the enter-key doesn't work with the element.<button>
<a href="...">
I'm a link wrapped in a button. And I don't really work
</a>
</button>
Phrasing content, but there must be no interactive content descendant and no descendant with the tabindex attribute specified.
<label htmlFor="checkbox">
I also want to be a button
</label>
<input
id="checkbox"
type="checkbox"
checked />
<button>
-element. It has everything out of the box: the role of a button to convey its semantics, tab index to put it into tab order, and easiness of giving it a click handler that handles the keyboard interaction as well.<button onClick={...}>
I am actual button
</button>