How can a click handler support a keyboard-operated page control?
Quick answer
A click handler alone does not make every element keyboard-operable. For an ordinary action, prefer a native <button>: keyboard-generated clicks apply to elements with a default key handler. Adding tabindex="0" to a custom element makes it focusable, not keyboard-operable; add an appropriate keyboard handler as well. The handler should follow the intended interaction for that control. [S1][S2]
The question, the details, the next step.
A practical checklist
Choose a native button for an action or a link for navigation when that fits the control. [S2]
Keep the action logic in one function and connect the native control’s click handler to it.
If using a custom element, make it keyboard-focusable and add an appropriate keyboard event handler;
tabindex="0"alone is not enough. [S2]Check that keyboard users can reach the control and trigger its intended action.
Treat custom keyboard handling as specific to the interaction, not as a universal recipe.
A few more useful answers
Does adding `tabindex="0"` make a custom control keyboard-operable?
Will a native button’s click handler respond to keyboard activation?
Does this example cover every custom widget’s keyboard behavior?
No. It shows how to keep the action in one function, but leaves the key handling specific to the control. The guidance calls for an appropriate handler without defining every widget’s key mappings. [S2]
Sources
References used for this guide.
- [S1] Element: click event — Web APIs | MDN developer.mozilla.org · Checked
- [S2] Keyboard accessible — Accessibility | MDN developer.mozilla.org · Checked
