Categories
JavaScript Answers

How to make a div into a link with JavaScript?

Spread the love

To make a div into a link with JavaScript, we can make it navigate to a URL on click.

For instance, we write

<div
  onclick="location.href='http://www.example.com';"
  style="cursor: pointer"
></div>

to make the div navigate to http://www.example.com on click by setting location.href to 'http://www.example.com' in the onclick attribute.

And we set the cursor style to pointer to change the cursor to a pointer when we move our mouse over the div.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *