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?
 
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.