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


To get the children of the $(this) selector, use the jQuery find() method. You can try to run the following code to get the children of the $(this) selector −

Example

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>
  $(document).ready(function(){
    $('#mydiv').click(function(){
      alert($(this).find('img:last-child').attr('src'));
   });
  });
</script>
<style>
  #my_div {
   width: 200px;
   height: 200px;
   background-color: red;
  }
</style>
</head>

<body>
<div id='mydiv'>
  <img src='https://www.tutorialspoint.com/videotutorials/images/tutorial_library_home.jpg' width='200' height='200' alt='Tutorials Library'>
  <img src='https://www.tutorialspoint.com/videotutorials/images/tutor_connect_home.jpg' width='200' height='200' alt='Video Tutorials'>
</div>
</body>
</html>

Updated on: 20-Jun-2020

72 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements