org.sonar.l10n.javascript.rules.javascript.S6847.html Maven / Gradle / Ivy
Non-interactive HTML elements, such as <div>
and <span>
, are not designed to have event handlers. When these
elements are given event handlers, it can lead to accessibility issues.
Why is this an issue?
Attaching event handlers to non-interactive HTML elements can lead to significant accessibility issues. These elements, such as
<div>
and <span>
, are not designed to interact with assistive technologies like screen readers, making it
difficult for users with disabilities to navigate and interact with the website. Additionally, these elements may not be focusable or provide visual
feedback when interacted with, resulting in a confusing and potentially frustrating user experience. Therefore, to maintain an accessible and
user-friendly website, event handlers should be used exclusively with interactive elements.
The rule only considers the handlers onClick
, onMouseDown
, onMouseUp
, onKeyPress
,
onKeyDown
, and onKeyUp
.
How to fix it
To fix this issue, remove the event handler from the non-interactive element and attach it to an interactive element instead. If the element is not
interactive, it should not have an event handler.
Code examples
Noncompliant code example
<li onClick={() => void 0} />
<div onClick={() => void 0} role="listitem" />
Compliant solution
<div onClick={() => void 0} role="button" />
<div onClick={() => void 0} role="presentation" />
<input type="text" onClick={() => void 0} /> // Interactive element does not require role.
<button onClick={() => void 0} className="foo" /> // button is interactive.
<div onClick={() => void 0} role="button" aria-hidden /> // This is hidden from the screenreader.
Resources
Documentation