Exercise objective
In this exercise, you’ll learn why native HTML buttons are superior to custom div-based buttons, and how to properly implement an accessible custom button if you absolutely need one.The challenge
You’ll find a<div> element on the introduction page that needs to be converted into a fully functional, accessible button.
The problematic div
Here’s what you’re starting with:- No semantic meaning (screen readers don’t know it’s a button)
- Not keyboard accessible (can’t be reached with Tab key)
- No keyboard interaction (Enter/Space keys don’t work)
- Not included in the focus order
- No role announcement for assistive technologies
Your task
Locate the exercise file
Navigate to
src/exercises/custom-button/custom-button.js in the course repository.Make it keyboard accessible
Add the element to the tab order:The
tabindex="0" value places the element in the natural tab order of the page.Complete solution
Here’s the complete JavaScript solution:custom-button.js
The better solution
Here’s why native buttons are better:What native buttons give you for free
Semantic role
Native buttons automatically have
role="button" and are announced correctly by screen readers.Keyboard support
Enter and Space keys work automatically without any JavaScript.
Focus management
Buttons are in the tab order by default with proper focus indicators.
Form integration
Native buttons can submit forms and have
type, disabled, and name attributes.Testing checklist
Verify your button implementation:Screen reader testing
Screen reader testing
- Button is announced as a “button” by the screen reader
- Button text/label is read correctly
- Screen reader indicates when the button is focused
- Screen reader announces button state changes (if applicable)
Keyboard testing
Keyboard testing
- Button can be reached using the Tab key
- Button activates when pressing Enter key
- Button activates when pressing Space key
- Button has a visible focus indicator
- Tab order makes logical sense
Mouse testing
Mouse testing
- Button responds to mouse clicks
- Hover states are visible (if styled)
- Cursor changes to pointer on hover
Visual testing
Visual testing
- Button looks interactive (not like plain text)
- Focus indicator is visible and meets contrast requirements
- Button is clearly distinguishable from surrounding content
Common mistakes to avoid
Learn more
Button patterns
Learn more about accessible button patterns and best practices