How to get children id when I click the parent?

You can pass your clicked element by dataadd(this);

You may wonder how this comes inside the method. adding this keyword like above will refer to the current element.

Then using that element, you can find children and with each you can grab other details by loop through the children.

function dataadd(ele){
 var children = $(ele).find('div');
    children.each(function(idx, element){
      console.log($(element).attr('id'));
 
    });
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="big" onclick="dataadd(this)">
   <div id="a">child1</div>
   <div id="b">child2</div>
</div>

Leave a Comment