How to get the children of $(this) selector?


To get the children of the $(this) selector in jQuery, use the find() method with each() method. Let us first see how to add jQuery:

Example

You can try to run the following code to get the children of the $(this) selector:

Live Demo

<html>
   <head>
      <title>jQuery Example</title>
      <script src = "https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
      <script>

         // Set the click handler on your div
         $("body").off("click", "#mydiv").on("click", "#mydiv", function() {
         
           // Find the image using.find() and .each()
           $(this).find("img").each(function() {
         
              var img = this;
         
           });
         });
         
      </script>
      <style>
         #mydiv {
            vertical-align: middle;
            background-color: #Ffff76;
            cursor: pointer;
            padding: 20px;
         }
      </style>
   </head>
   <body>
      <div id="mydiv">
         <img src="/green/images/logo.png" width="300" height="100"/>
      </div>
   </body>
</html>

Updated on: 18-Dec-2019

57 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements