How to make entire Div clickable in javascript

Created at 07-Jan-2022 , By samar

How to make entire Div clickable in javascript

In this session, we will try our hand at solving the "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.
  • 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>
    

    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

If you like what you are reading, please consider buying us a coffee ( or 2 ) as a token of appreciation.

Buy Me A Coffee

Don't forget to share this article! Help us spread the word by clicking the share button below.

We appreciate your support and are committed to providing you valuable and informative content.

We are thankful for your never ending support.