To <button> or not to <Button>

A presentation at Various Talks in April 2024 in by Todd Libby

Slide 1

Slide 1

TO <button> OR NOT To <Button>? THAT is the question.

Slide 2

Slide 2

BUTTON SCREEN READERS ANNOUNCE THE NESTED TEXT FOR THE <button> ELEMENTS, AND value ATTRIBUTE FOR INPUT BUTTONS. FORM BUTTONS MUST NEVER BE EMPTY.

Slide 3

Slide 3

WHEN DO I USE A BUTTON?

Slide 4

Slide 4

WHEN YOU ARE COMPLETING AN ACTION SUBMITTING A FORM, A SEARCH QUERY, COMPLETING AN ORDER

Slide 5

Slide 5

NOT! LINKING TO ANOTHER PAGE.

Slide 6

Slide 6

NOT! A SPAN OR A DIV!

Slide 7

Slide 7

Slide 8

Slide 8

SPAN, DIV, AND IMG ELEMENTS ARE NOT FOCUSABLE UNLESS THEY HAVE A TABINDEX=”0″ ATTRIBUTE/VALUE APPLIED. TABINDEX=”0″ ONLY LETS THE USER TAB TO THE CONTROL BUT IT DOES NOT LET THE ONCLICK EVENT MAGICALLY WORK WITH ENTER AND SPACEBAR KEYS. YOU NEED A <BUTTON> FOR FREE KEYBOARD ACCESSIBILITY.

Slide 9

Slide 9

BUTTONS ARE NOT USED TO OPEN WEBPAGES, INSTEAD USE LINKS. BUTTONS PERFORM AN ACTION, LINKS TAKE YOU SOMEWHERE, DIVS HOLD THINGS.

Slide 10

Slide 10

Slide 11

Slide 11

INPUT TYPE VS BUTTON TYPE

Slide 12

Slide 12

INPUT TYPE WHEN A USER ENTERS INFORMATION, THAT INFORMATION IS BEING SENT BACK TO A SERVER

Slide 13

Slide 13

BUTTON TYPE WHEN YOU NEED STYLING CONTROL OR HAVE TO EMBED MORE THAN JUST A TEXT STRING

Slide 14

Slide 14

ROLE=“BUTTON” THIS DOES NOT WORK! IT MAKES IT WORSE.

Slide 15

Slide 15

IF YOU DO THIS…

Slide 16

Slide 16

<div onclick=”DoThing();” onkeypress=”DoThing();” tabindex=”0” role=”button”>Do a thing.</div>

Slide 17

Slide 17

Slide 18

Slide 18

SEMANTIC HTML KEYBOARD USERS WILL THANK YOU

Slide 19

Slide 19

BUT… BUTTONS SOMETIMES LINKS LOOK LIKE BUTTONS AND BUTTONS LOOK LIKE LINKS!

Slide 20

Slide 20

STOP IT! MAKE LINKS LOOK LIKE LINKS (A DIFFERENT PRESENTATION) MAKE BUTTONS LOOK LIKE BUTTONS (CUZ THEY’RE BUTTONS!)

Slide 21

Slide 21

EXAMPLES https://medium.com/simple-human/but-sometimes-links-looklike-buttons-and-buttons-look-like-links-9b371c57b3d2

Slide 22

Slide 22

ARIA Aria-pressed, aria-expanded, aria-disabled

Slide 23

Slide 23

ARIA-PRESSED

Slide 24

Slide 24

The aria pressed attribute defines the button as a toggle button. The value describes the state of the button. The values include aria pressed=”false” when a button is not currently pressed, aria pressed=”true” to indicate a button is currently pressed, and aria pressed=”mixed” if the button is considered to be partially pressed. If the attribute is omitted or set to its default value of aria pressed=”undefined”, the element does not support being pressed.

Slide 25

Slide 25

ARIA-EXPANDED

Slide 26

Slide 26

If the button controls a grouping of other elements, the aria expanded state indicates whether the controlled grouping is currently expanded or collapsed. If the button has aria expanded=”false” set, the grouping is not currently expanded; If the button has aria expanded=”true” set, it is currently expanded; if the button has aria expanded=”undefined” set or the attribute is omitted, it is not expandable.

Slide 27

Slide 27

ARIA-DISABLED

Slide 28

Slide 28

The aria disabled state indicates that the element is perceivable but disabled, so it is not editable or otherwise operable.

Slide 29

Slide 29

BUTTON = ACTION A = TAKES YOU PLACES DIV = CONTAINER

Slide 30

Slide 30

THANKS! @TODDLIBBY - TODD LIBBY