95

How do I make an entire DIV a clickable hyperlink. Meaning, I essentially want to do:

<div class="myclass" href="example.com">
    <div>...</div>
    <table><tr>..</tr></table>
    ....
</div>

And whenever someone mouse hovers of the myclass DIV, I want the entire DIV it to be a clickable hyperlink.

Muhammad Imran Tariq
  • 22,654
  • 47
  • 125
  • 190
Teddyk
  • 961
  • 1
  • 6
  • 4

7 Answers7

177

You can add the onclick for JavaScript into the div.

<div onclick="location.href='newurl.html';">&nbsp;</div>

EDIT: for new window

<div onclick="window.open('newurl.html','mywindow');" style="cursor: pointer;">&nbsp;</div>
Gianfranco P.
  • 10,049
  • 6
  • 51
  • 68
Joel Etherton
  • 37,325
  • 10
  • 89
  • 104
  • 5
    When I do that, my mouse cursor doesn't change to a hand like it does when hovering over a link. What am I doing wrong? – Teddyk Feb 02 '10 at 22:42
  • You would need to use window.open. There are many canned scripts that perform this action. http://www.javascript-coder.com/window-popup/javascript-window-open.phtml – Joel Etherton Feb 02 '10 at 22:45
  • 2
    if you modified the move to be, cursor:pointer - that works – Teddyk Feb 02 '10 at 22:49
  • 5
    Use `cursor: pointer` instead of `cursor: hand`. – bazzilic Nov 10 '14 at 12:07
  • 1
    That wouldn't be very good for SEO. Search engines wouldn't see the hyperlink if it was coded in JavaScript. – Richard Moore Dec 10 '15 at 11:09
  • https://www.w3.org/WAI/WCAG21/Techniques/failures/F59 – danielnixon Jul 03 '19 at 09:57
  • @danielnixon: That's a fantastic resource and something any developer should consider when creating a user interface of any kind through the web. This question didn't have any such considerations and asked only "how to do it". Perhaps your comment is best suited at the question level because it is certainly relevant. – Joel Etherton Sep 23 '19 at 14:41
17

You just need to specify the cursor as a pointer, not a hand, as pointer is now the standard, so, here's the example page code:

<div onclick="location.href='portable-display-stands.html';" id="smallbox">The content of the div here</div>

and the example CSS:

#smallbox {
    cursor: pointer;
}

So the div is now a clickable element using 'onclick' and you've faked the hand cursor with the CSS...job done, works for me!

Nick Annies
  • 179
  • 1
  • 2
12

This is a late answer, but this question appears highly on search results so it's worth answering properly.

Basically, you shouldn't be trying to make a div clickable, but rather make an anchor div-like by giving the <a> tag a display: block CSS attribute.

That way, your HTML remains semantically valid and you can inherit the typical browser behaviours for hyperlinks. It also works even if javascript is disabled / js resources don't load.

Jimmy Breck-McKye
  • 2,923
  • 1
  • 22
  • 32
7

Add an onclick to your DIV tag.

http://webdevjunk.com/coding/javascript/3/use-onclick-to-make-entire-div-or-other-html-object-into-a-link/

Keith Adler
  • 20,880
  • 28
  • 119
  • 189
4

Why don't you just do this

<a href="yoururl.html"><div>...</div></a>

That should work fine and will prompt the "clickable item" cursor change, which the aforementioned solution will not do.

Eonasdan
  • 7,563
  • 8
  • 55
  • 82
cfg
  • 954
  • 7
  • 6
1

alternative would be javascript and forwarding via the onclick event

<div onclick="window.location.href='somewhere...';">...</div>
tDo
  • 528
  • 3
  • 5
  • When I do that, my mouse cursor doesn't change to a hand like it does when hovering over a link. What am I doing wrong? – Teddyk Feb 02 '10 at 22:41
  • onclick actually does not make it a link but just clickable. You could use the css cursor attribute to change it likewise. – tDo Feb 03 '10 at 01:39
  • https://www.w3.org/WAI/WCAG21/Techniques/failures/F59 – danielnixon Jul 03 '19 at 09:58