Make Entire Div Clickable

Avatar of Chris Coyier
Chris Coyier on

Update November 2020: I think the best possible technique for this is Method 4 in this article. The <div> (or whatever wrapper element) remains semantic and accessible, while being “clickable” over the whole area. It doesn’t break text selection and respects other “nested” interactive elements.

This is perfectly valid HTML:

<a href="http://example.com">
  <div>
     anything
  </div>
</a>

And remember you can make links display: block;, so the whole rectangular area becomes “clickable”. But, if there is a ton of content in there, it’s absolutely horrible for accessibility, reading all that content as the interactive link.

If you absolutely need to use JavaScript, one way is to find a link inside the div and go to its href when the div is clicked. This is with jQuery:

$(".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 data-location="http://place.com"> and do like:

window.location = $(".myBox").data("location");