To include both href and onclick in an HTML a tag with JavaScript, we add both attributes.
For instance, we add a link with
<a href="www.example.com" onclick="return onClick();">Item</a>
We define the onClick
function with
function onClick() {
// ...
}
We set onclick
to return the return value of onClick
so that if onClick
returns false
, the link won’t navigate to the href URL.
onClick
is called when the link is clicked.