Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Can a tag be a link without using the `a` tag?

I was just curious to know can a tag like div or h2 or any other tag be a link without using the a tag.

Thanks in advance!

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

The simplest way is to use onclick="window.location.assign()"

The snippet below addresses styling, tab accessibility, and browser history concerns, but it is invalid by WCAG.

document.querySelector('.link').addEventListener('keypress', function(e) {
  if (e.key === 'Enter') {
    window.location.assign('https://stackoverflow.com/');
  }
});
.link {
  color: blue;
  text-decoration: underline;
}

.link:hover {
  color: darkblue;
  cursor: pointer;
}
<div tabindex="1" class="link" onclick="window.location.assign('https://stackoverflow.com/');">
  Fake Link
</div>
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading