Code Snippets Gallery

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>

Responses

  1. Dyllon says:

    works very nicely. thanks.

  2. Nathan says:

    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?

  3. Very cool! Just right now I have used this in my project :D

Leave a Comment

Remember:
  • Be nice.
  • Wrap all (unescaped) code in <pre> and <code> tags. (single or multiple lines). Don't use <?php ?> tags.
  • You may use regular HTML stuff like <a href="">, <em>, and <strong>