How to make entire Div clickable in javascript

There are lots of method available to make a div clickable in Javascript. But here we have to get the link of anchor tag which is the inner element of div tag and redirect the user to the link using window.location.

Answers 1
  • Make a div clickable using inner anchor tag with javascript

    <!-- HTML -->
    <div class="myBox">
      blah blah blah.
      <a href="http://google.com">link</a>
    </div>
    
    <!-- javascript -->
    <script>
    $(".myBox").click(function() {
      window.location = $(this).find("a").attr("href"); 
      return false;
    });
    </script>
    
    -1

    You can make the entire div clickable using the value of href attribute of anchor tag which is child element of div.

  • Back to code snippet queries related javascript