views:

81

answers:

3

it works fine for one div

(function($) { 
     $(document).ready(function(){
        $('#div1').hide();
        $('a.plus').click(function(){
           $('#div1').slideToggle();
           $(this).toggleClass("minus_icon"); 
           return false;
        });
     });
})(jQuery);

How to make this slide toogle effect for multiple div on same page? I have multiple divs with different id to hide like #div1, #div2, div#3.

edit:

This is situation in html

<a class="plus"> more details </a>
<div id="div1" class="description">
<p> Hidden content here </p>
</div>

<a class="plus"> more details </a>
<div id="div2" class="description">
<p> Hidden content here </p>
</div>

<a class="plus"> more details </a>
<div id="div3" class="description">
<p> Hidden content here </p>
</div>
A: 

Give your divs the same class (like 'div_to_toggle') and then instead of $('#div1'), $('#div2'), ... you can use one selector: $('.div_to_toggle') that will select them all:

$('.div_to_toggle').hide();
$('.div_to_toggle').slideToggle();
zifot
right now when i click on <a> for one box it opens all boxes. now i'm going to try ur solution
metal-gear-solid
Mind you..he's changing the class of those div time to time..
Kai
@zifot - it's not working . when i click on <a> for one box it opens all boxes.
metal-gear-solid
+2  A: 

Presuming your HTML is something like this:

<div id="div1" class="more">Content</div>
<a class="plus">Show More</a>

<div id="div2" class="more">Content</div>
<a class="plus">Show More</a>

<div id="div3" class="more">Content</div>
<a class="plus">Show More</a>

Then having a function like this will work for as many a.plus / div pairs you have:

(function($) { 
 $(document).ready(function(){
    $('div.more').hide();
    $('a.plus').click(function(){
       $(this).prev().slideToggle(); // if your divs are after the plus, use next() instead
       $(this).toggleClass("minus_icon"); 
       return false;
    });
 });
})(jQuery);

If your a.mores are, for whatever reason, nowhere near the divs themselves, you will have to set an attribute on the a.more to link it to a div, eg. (HTML)

<a class="plus" data-article="1">Show More</a>
<a class="plus" data-article="2">Show More</a>
<a class="plus" data-article="3">Show More</a>

... some more HTML

<div id="div1" class="more">Content</div>
<div id="div2" class="more">Content</div>
<div id="div3" class="more">Content</div>

and Javascript:

(function($) { 
 $(document).ready(function(){
    $('div.more').hide();
    $('a.plus').click(function(){
       var id = 'div' + $(this).attr('data-article');
       $('div#' + id).slideToggle();
       $(this).toggleClass("minus_icon"); 
       return false;
    });
 });
})(jQuery);
Adam Hepton
Thanks for your answer and good effort but i don't want to add non valid `data-article="1"`
metal-gear-solid
You can modify Adam's solution slightly - instead of using data-article attributes, give your a tags with class="plus" additional id with numbers corresponding to appropriate divs like this:<a class="plus" id="plus2">Show More</a>and then use var id = 'div'+substr($(this).id, 4);(hope I got params to substr function correct).
zifot
+2  A: 

Hi!

(function($) { 
     $(document).ready(function(){
        $('#div1, #div2, #div3').hide();
        $('a.plus').click(function(){
           $(this).next().slideToggle();
           $(this).toggleClass("minus_icon"); 
           return false;
        });
     });
})(jQuery);
HiddenChilli
@HiddenChilli - thanks it's working. can we do one more thing. I want to make it dynamic. see i'm using here div like div1 div2 div3. is it possible to have same effect using @div only like `div'+substr($(this).id, 4)`
metal-gear-solid
for example my id of `div` will always have in same format `div1`, `div2` or `div11`, `div12` . so i want to make this code dynamic then there will be no need to modify jquery file ,when i will add more `div` in `HTML`. will be easier for my client.
metal-gear-solid
oh i see do you mean $('div.description').hide()?
HiddenChilli
yes or any other method which can do this
metal-gear-solid
try this: $('a.plus').next().hide();
HiddenChilli
cool - it's working . great help. Thanks
metal-gear-solid