Offering the same experience with the mouse and the keyboard allows users to pick their preferred device.
Additionally, users of assistive technology will be able to browse the site even if they cannot use the mouse.
This rule raises an issue when an element recognized by the analyzer as native HTML:
onClick attribute but no onKeyDown or onKeyUp attribute.onMouseover attribute but no onFocus attribute.onMouseout attribute but no onBlur attribute.The rule recognizes plain HTML attributes (such as onClick), Angular event bindings (such as (click),
ng-click, and on-click), and Vue bindings (such as v-on:click and @click). These bindings can
include event modifiers such as @click.prevent and @keydown.enter.
Only elements the analyzer recognizes as native HTML are checked. Components and custom elements are skipped because the analyzer cannot determine how they render or handle keyboard events.
In the case of onClick, the equivalent keyboard handler should activate on the "Enter" and "Space" keys, since these are the keys
assistive technologies use to activate buttons.
onKeyPress is also recognised as a keyboard equivalent for backward compatibility, but the event is deprecated and should not be used in new code; prefer
onKeyDown.
<div onClick="doSomething();" ...> <!-- Noncompliant - 'onKeyDown' or 'onKeyUp' missing --> <a onMouseover="highlight();" ...> <!-- Noncompliant - 'onFocus' missing --> <a onMouseout="unhighlight();" ...> <!-- Noncompliant - 'onBlur' missing -->
The most accessible fix is to use the native interactive element. A <button> is focusable, exposes the right role to assistive
technology, and is activated by both Enter and Space without extra code:
<button onClick="doSomething();">Do something</button> <!-- Compliant -->
If a native non-interactive element, such as a <div>, handles clicks, the equivalent keyboard handler must check the key so it
only triggers on Enter or Space, and the element must expose role="button" and tabindex="0" so it is announced and
reachable:
<div role="button" tabindex="0" onClick="doSomething();"
onKeyDown="if (event.key === 'Enter' || event.key === ' ') doSomething();"> <!-- Compliant -->
</div>
The same applies to framework event bindings:
<div (click)="doSomething()" (keydown.enter)="doSomething()" role="button" tabindex="0"></div> <div @click="doSomething()" @keydown.enter="doSomething()" role="button" tabindex="0"></div>
For purely visual hover effects, prefer CSS over scripted mouse handlers — the :hover and :focus-visible pseudo-classes
give mouse and keyboard parity without any JavaScript:
<a href="/page" class="link-with-hover">Link</a> <!-- Compliant - hover styling handled by CSS :hover / :focus-visible -->
When scripted hover behaviour is genuinely needed, pair onMouseover / onMouseout with onFocus /
onBlur:
<a href="/page" onMouseover="highlight();" onFocus="highlight();"
onMouseout="unhighlight();" onBlur="unhighlight();">Link</a> <!-- Compliant -->
The following native interactive elements are skipped when they carry an onClick attribute, because keyboard activation is built into
the platform:
<input type="button"> and <input type="submit">,<button>,<a>,<summary>.Native controls with an implicit textbox, checkbox, radio, combobox, or listbox
role are also skipped. This includes text inputs, <textarea>, <input type="checkbox">, <input
type="radio">, and <select>. A <select> has the implicit combobox role by default and the
listbox role when it has the multiple attribute or a size greater than 1.
Be aware that the anchor exception is broad: only an <a> with an href attribute is actually focusable and activated
by Enter — an <a onClick> without href behaves like inert text from the keyboard. For button-like actions inside the
page, prefer <button> over a bare <a>.
An issue will still be raised for any element that carries role="button" (including <button role="button">),
because the explicit role overrides the implicit behaviour and the element is treated as a custom widget.
The whitelistedElements parameter takes a comma-separated list of native HTML tag names to exclude from all checks by this rule. It is
empty by default.
keypress event (deprecated)<a> elementbutton
role