Get dynamically created parent and child child element

0

I have the following question: I create several id and class dynamically.

What I need is when I click on the parent id the child receives a toggle.

Super parent receives via append the divs.

When I click on "press" the 39 daughter (of 39) has to suffer a toggle. When I click on "tighten" from 40 the daughter (of 40) has to suffer a toggle, and so on (I'll have lots of it on the same page).

<div id="superpai">
 <div id="39"> aperte 
         <div class="filha">39</div>
</div>
<div id="40"> aperte 
         <div class="filha">40</div>
</div>
<div id="41"> aperte 
         <div class="filha">41</div>
</div>

</div>

What would be the simplest solution to this? When I know the parent element is pretty simple. But when I do not know (that's the case). How do I?

What's the best way?

    
asked by anonymous 07.04.2016 / 15:41

1 answer

1

What you are looking for might look like this:

$(document).on('click', '#superpai > div', function() {
  $(this).find('.filha').toggle();
});

It is good to use class .filha so at the time that click happens jQuery searches for an element with that class, descending from the clicked element.

The delegator here is .on() already explained here . That is, when there is a click on the page, jQuery looks for div elements that are direct descendant from #superpai .

jsFiddle: link

    
07.04.2016 / 17:13