How to use jQuery to switch between a plus and minus sign on collapse and expand?
1. Add Jquery script ignore if already added
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.0/jquery.min.js"></script>
2. Add Javascript to show hide
<script type="text/javascript">
$(".question").click(function () {
if ($(this).next(".answer").is(":hidden")) {
$(this).next(".answer").slideDown("slow");
$(this).children('span').text('-');
} else {
$(this).next(".answer").slideUp("slow");
$(this).children('span').text('+');
}
});
</script>
3. Add content as like given below
<div class="question">
<span style="font-size: 30px;">+</span><h1 style="display: inline !important;">Heading - 1 </h1>
</div>
<div class="answer">
<ul class="sub-menu">
<li>Line 1</li>
<li>Line 2</li>
<div class="question">
<span style="font-size: 30px;">+</span><h2 style="display: inline !important;">Inner Heading 1</h2>
</div>
<div class="answer">
<li>Child - Line 1</li>
<li>Child - Line 2</li>
</div>
</ul>
</div>
That's All, Enjoy
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.0/jquery.min.js"></script>
2. Add Javascript to show hide
<script type="text/javascript">
$(".question").click(function () {
if ($(this).next(".answer").is(":hidden")) {
$(this).next(".answer").slideDown("slow");
$(this).children('span').text('-');
} else {
$(this).next(".answer").slideUp("slow");
$(this).children('span').text('+');
}
});
</script>
3. Add content as like given below
<div class="question">
<span style="font-size: 30px;">+</span><h1 style="display: inline !important;">Heading - 1 </h1>
</div>
<div class="answer">
<ul class="sub-menu">
<li>Line 1</li>
<li>Line 2</li>
<div class="question">
<span style="font-size: 30px;">+</span><h2 style="display: inline !important;">Inner Heading 1</h2>
</div>
<div class="answer">
<li>Child - Line 1</li>
<li>Child - Line 2</li>
</div>
</ul>
</div>
That's All, Enjoy
Comments
Post a Comment