Update: this probably isn’t a thing you need to rely on JavaScript for anymore. Since HTML5, this is perfectly valid:
<a href="http://example.com">
<div>
anything
</div>
</a>
And remember you can make links display: block;
so sometimes you don’t even need the div.
But if you absolutely need to use JavaScript, one way is to find a link inside the div and go to it’s href when the div is clicked.
$(".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>
Or, you could set a data-attribute on the div and do like:
window.location = $(this).data("location");
- Tech Tricks answered 2 years ago
- You must login to post comments
Your Answer
Please login first to submit.