Web Design Blog

This is where we store some of our Web Development thoughts, tips and tricks, just because we like to share.

Javascript – Make Div Into A Clickable Link

From what I’m aware, you can’t make a div into a clickable link with just HTML. The only way I’ve ever known is by using Javascript.

Open in new window

Here’s how to make a div into a clickable link that will open the target URL into a new window.

1
2
3
4
 
<div onclick="window.open('http://www.brightcherry.co.uk/');">
 
</div>

Open in same window

Here’s how to make a div into a clickable link that will open the target URL into the same window.

1
2
3
4
 
<div onclick="window.location = 'http://www.brightcherry.co.uk/';">
 
</div>

Notes

The links CAN be relative to the domain, they don’t just work with full URLs. You can use URLs like “/dir/index.html”

30 Mar 2011 / 2 Comments / Javascript / by Maruf

2 Comments

  1. 20/05/2011
    1

    to make a div clickable. put an a tag inside it and display it as a block element and give it the same width and height as the div I do it all the time.

  2. 17/06/2011
    2

    Just a usability tweak to this… add style=”cursor:pointer;”. This way when you hover over it the user realises it’s a link.

    Personally I also add a link into the div for the user to click on should there be no Javascript support, that’s me being paranoid though.

    Javascript was my only solution to make the div a link as I have block elements in there (headers), and you can’t wrap a block element in a link.

Leave a Reply

© 2012 BrightCherry :)