One solution would be to add a new "internal element" such as a <span>
inside of the <a>
element, and bind the onClick
event handler with event.stopPropagation()
to that internal element.
This would cause the <span>
to intercept and stop the propagation of the click event before it bubbles up to the parent <a>
(which would by default cause the browser to navigate to the href
url).
You should find this method still preserves visibility of the url assigned to the href
attribute of <a>
for the user when it is hovered with the mouse cursor:
<a href={menuItem.url}>
<span onClick={e => {
// Intercepts and prevents the event from propagating up to the parent <a>
e.stopPropagation();
// Your existing logic for the onClick event
var checkedItems = document.querySelectorAll("input:checked") as NodeListOf<HTMLInputElement>;
for (let i = 0; checkedItems[i]; i++) {
checkedItems[i].checked = false;
}
window.location.href = menuItem.url;
}}> The text label for your link </span>
</a>
For this method to work, it assumed that there is no padding between the box border (outer boundary) of the <a>
and the box border of the inner <span>
. Here's a jsFiddle (non-jsx) demonstrating the general idea.
Hope that helps!