Code Snippet
Make Entire Div Clickable
$(".myBox").click(function(){
window.location=$(this).find("a").attr("href");
return false;
});Looks for a link inside div with class of "myBox". Redirects to that links value when anywhere in div is clicked.
Reference HTML:
<div class="myBox">
blah blah blah.
<a href="http://google.com">link</a>
</div>
works very nicely. thanks.
Chris, thanks for this. When I tried this it worked for a hover over empty space in the div but not for the link itself; when I clicked on the link itself it went to a 404 error. Any ideas?
Very cool! Just right now I have used this in my project :D
Very very nice.
I had a job interview today. When asked if there were any blogs I read regularly, I mentioned css-tricks, and the interviewer said that was one of his faves, too.
Great technique.
What if you want to have other links inside the linked div? How would you keep those working still?
this is very useful for my current project
Once again, TOTALLY AWESOME!!!
“window.location” seems not to work in IE if you are using RealURL. Any Ideas?
Thx
Christoph
fixed it this way:
var base = $j(“base”).attr(“href”);
$j(“#menu li a”).each(function(){
$j(this).attr(“href”, base+$j(this).attr(“href”));
});
I think i am going to try this with tables…. Or is there an easier way?
did it work on thie table.
wow … very sweet idea :- )
thanks
Nice work
really helpful
There’s a pure CSS way to go about doing this. Just have an anchor (a) tag within the DIV that links to the URL you want. Then, set it to a block display, relative positioning, left and top equal to 0, width and height to full (or use jQuery to match all A’s within DIV’s to the same dimensions as the DIV).
Great Solution! This really helped.
This is awesome. I’ve been trying to figure out a better way to do this for a while.
How can I ensure that the href title appears when I hover on the div and also that the URL appears in the status bar of the browser I’m using?
Very nice thanks :D
fasf
Its much nicer this way and not having to wrap a whole div in an A tag.
For usability (dependent on what you are using it for), remember to add cursor:pointer in your css. Nice simple bit of code thanks.
Hi,
I want to make entire div clickable, but my div is consisting of news ticker and each of then linked to different URL, will this solution work in these situation?
Thanks,
Dearthelusion
How about opening in a new window?
oops, didn’t mean that to be a direct reply =]
I’m new to scripting in jQuery. How would I set the target to be a new window?
Any help would be greatly appreciated!
@l3v1tcl30s
DigWP
A book and blog co-authored by Jeff Starr and myself about the World's most popular publishing platform.
Quotes on Design
Design, like Art, can be an elusive word to define and an awfully fun thing to have opinions about.
HTML-Ipsum
One-click copy to clipboard access to Lorem Ipsum text that comes wrapped in a variety of HTML.
Bookshelf
Hey Chris, what books do you recommend? These, young fertile mind, these.