Skip to content Skip to sidebar Skip to footer

Single Function To Toggle Multiple Divs Inside Other Divs Individually

I have some divs in my page, and each one of them has got a content I want to toggle via jQuery. This is actually how my code looks like: $('.my_div').click(function() { $( '.my

Solution 1:

You can check if div has specific class value like:

//select div with class attribute starts with my_div
$('div[class^="my_div"]').click(function() {
  //find child element div with class start my_div_B
  $(this).children("div[class^='my_div_B']").slideToggle("slow");
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="my_div">
  <img src='http://placehold.it/200x100' />
  <img src='http://placehold.it/200x100' />
  <div class="my_div_B">text</div>
</div>
<div class="my_div2">
  <img src='http://placehold.it/200x100' />
  <img src='http://placehold.it/200x100' />
  <div class="my_div_B2">text</div>
</div>

Post a Comment for "Single Function To Toggle Multiple Divs Inside Other Divs Individually"