views:

250

answers:

3

Hello guys,

I was wondering how to toggle a "child" div only, clicking in a button which repeats over the html, like so :

<div class="button">
     <div class="hide toggle">
          Blah1
     </div>
</div>
<div class="button">
     <div class="hide toggle">
          Blah2
     </div>
</div>
<div class="button">
     <div class="hide toggle">
          Blah3
     </div>
</div>

The goal would be to affect only the inmediate child div, and keep hidden the rest.

I don't know if it's possible.

This bit of code was toggling me all of the divs :

<script type="text/javascript">
  $(document).ready(function(){

    $('.button').click(function() {
    $('.toggle').slideToggle();
    }, function () {
        $('.toggle').slideToggle();
    });

  });
</script>

Many thanks for your help.

+2  A: 

try

$(this).next('.toggle').slideToggle();

or

$(this).children('.toggle:first').slideToggle();
Thomas Stock
+2  A: 

Inside the click event handler the this keyword refers to the element that was clicked on. Replace your $('.toggle').slideToggle() lines with

$('.toggle',this).slideToggle();
Mario Menger
A: 

You guys your are dangerous cracks

Peanuts
thanks for the points :)
Thomas Stock