views:

170

answers:

4

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.

+5  A: 

You can put an <a> element inside the <div> and set it to display: block and height: 100%.

SLaks
That won't work because I have a lot of content inside the DIV. I'll update my original post to make it more descriptive
Teddyk
@Teddy: Adding `position:relative;` to the `div` and then `position: absolute; height: 100%;` to the `a` as well... should make the entire area of the `div` is active.
prodigitalson
You can put all of that content inside the `<a>`.
SLaks
Demo: http://jsbin.com/adoka/
SLaks
That is not true. `<a>` is an inline level element capable of holding only inline level elements. Which means that having `<div>` and/or `<table>` has children of an `<a>` is not valid XHTML. To workaround this limitation, use inline elements and CSS to display them as block level elements. See http://www.cs.sfu.ca/CC/165/common/ref/wdgxhtml10/inline.html for a list of inline elements.
Andrew Moore
FYI: If the `<a>` were made an overlay, as prodigitalson suggested, then none of the elements "behind" the `<a>` would be selectable.
Christopher Parker
+4  A: 

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/

Nissan Fan
Also, set the div's cursor style to pointer so the whole thing gets the finger.
Michael Itzoe
The sample at the link shows that setting as well.
Nissan Fan
+4  A: 

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: hand;">&nbsp;</div>
Joel Etherton
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
Also, how do I open the link in a new window?
Teddyk
Nothing, a div does not naturally have the change. You need also to have style="cursor: hand;" as an attribute.
Joel Etherton
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
@Teddyk: See edit.
Joel Etherton
if you modified the move to be, cursor:pointer - that works
Teddyk
A: 

alternative would be javascript and forwarding via the onclick event

<div onclick="window.location.href='somewhere...';">...</div>
tDo
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
onclick actually does not make it a link but just clickable.You could use the css cursor attribute to change it likewise.
tDo