expanding and collapsing jquery

I am using the below html and script for expanding and collapsing, but i am not able to extend it to multiple sliders...

HTML CODE:
<h4 id="expanderHead" style="cursor:pointer;">
      EXPANDING COLLAPSING DIV <span id="expanderSign">+</span>
</h4>
<div id="expanderContent" style="display:none">
      content<br />
      content<br />
      content<br />
      content<br />
      content<br />
</div>


Java Script:

<script type="text/javascript">
$(document).ready(function(){
      $("#expanderHead").click(function(){
            $("#expanderContent").slideToggle();
            if ($("#expanderSign").text() == "+"){
                  $("#expanderSign").html("−")
            }
            else {
                  $("#expanderSign").text("+")
            }
      });
});
</script>


If i want to write a html something like below... for multiple sliders...
 i am copying the same script again but with different variable (renamed expanderHead to expanderHead2 and duplicated the script with new name expanderHead2)
so  how can i expand the script for multiple variables with out duplicating the script

see my html below
there is an unique obj when ever it enters loop

while( $obj = sqlsrv_fetch_object( $stmt)) {
<h4 id=$obj->uniqueID style="cursor:pointer;">
      EXPANDING COLLAPSING DIV <span id=$obj->uniqueID>+</span>
</h4>
<div id=$obj->uniqueID style="display:none">
      content<br />
</div>

}
then how do i write script for this... how to send variables ....


if not do you know any other way for expander and collapsing for loops...

Thanks,
shragiAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Robert SchuttSoftware EngineerCommented:
You could use the unique IDs but I think it's easier to use classes, you may already need that for your styling anyway. Then use some jQuery methods to find the corresponding elements, this depends on your exact html. For the mark-up you posted you can use the code below. I also set up a jsFiddle for you.

$(document).ready(function(){
      $(".expanderHead").click(function(){
            $(this).next(".expanderContent").slideToggle();
            var sgn = $(this).children(".expanderSign");
            if (sgn.text() == "+"){
                  sgn.html("−")
            }
            else {
                  sgn.text("+")
            }
      });
});

Open in new window


So just change id to class in the mark-up (or add it so you can still use the unique IDs):
<h4 class="expanderHead" style="cursor:pointer;">
      EXPANDING COLLAPSING DIV <span class="expanderSign">+</span>
</h4>
<div class="expanderContent" style="display:none">
      content<br />
      content<br />
      content<br />
      content<br />
      content<br />
</div>
<h4 class="expanderHead" style="cursor:pointer;">
      EXPANDING COLLAPSING DIV 2 <span class="expanderSign">+</span>
</h4>
<div class="expanderContent" style="display:none">
      content2<br />
      content2<br />
      content2<br />
      content2<br />
      content2<br />
</div>
<h4 class="expanderHead" style="cursor:pointer;">
      EXPANDING COLLAPSING DIV 3 <span class="expanderSign">+</span>
</h4>
<div class="expanderContent" style="display:none">
      content3<br />
      content3<br />
      content3<br />
      content3<br />
      content3<br />
</div>

Open in new window


EDIT: with some more code and css it's possible to give all kinds of options using that html mark-up as you can see in this updated jsFiddle.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.