I have a working toggle function that expands and collapses but my list when expanded is very long and I wanted to put a “Close” link at the bottom to enable collapse so my users don’t have to scroll to top to click and collapse. Any ideas how I can modify this code to expand it’s functionality for my purposes?
<script language="javascript">
$(function(){
$(".formname").toggle(function(){
var id=$(this).attr('id');
$("#form"+id).fadeIn('slow');
},function(){
var id=$(this).attr('id');
$("#form"+id).fadeOut('slow');
});
});
</script>
my html looks like:
<ul>
<li><a href="#" id="formxyz" class="formname">My Expanding/Collapsing Data</a></li>
<div style="display:none;">
<ul>
<li>Content</li>
</ul>
</div>
</ul>
if I insert the same link li at the bottom before the closing ul the user has to click it twice to collapse. I’d prefer a single click solution.
<ul>
<li><a href="#" id="formxyz" class="formname">My Expanding/Collapsing Data</a></li>
<div style="display:none;">
<ul>
<li>Content</li>
<li><a href="#" id="formxyz" class="formname">Close</a></li>
</ul>
</div>
</ul>
If you use
toggle()event on formxyz, the new close action will break the original rule, therefore sometimes you have to click formxyz twice to show content.So use
click()event withfadeToggle()action on formxyz. It will show content while it’s hiding.Then add a class on all “close”, use
parentsmethod to find it’s parents div (it should be the content div), then hide it.The html:
The js file:
OR Check emulate code on jsFiddle. It’s executable.