Instead of .remove()
ing your topics you could hide()
them and then create a link/button in the questions area with a click()
handler that .show()
s the topics again.
Additionally:
- There's no good reason for you to be unbinding the click handler; that only serves to make them not be able to be clicked again until the javascript is reloaded.
- To make it infinitely nest-able with questions inside of questions inside of topics... you could just have a class like
.expandable
that is applied to a<div>
that wraps around a header element and any subelements and any<h#>
inside an.expandable
div is a clickable title to show the next tier and hide itself. - You only need
$(document).ready(function () {...});
once wrapping all of the code you want to execute at document ready time.
Here's an example of the nest-able solution with links to go back up a level:
<div class="expandable top">
<h1>Topic 1</h1>
<div class="expandable">
<h1>foo</h1>
<div class="expandable">
<h1>sub-foo</h1>
<div class="expandable">
Stuff
</div>
</div>
</div>
<div class="expandable">
<h1>bar</h1>
</div>
</div>
<div class="expandable top">
<h1>Topic 2</h1>
<div class="expandable">
<h1>foo2</h1>
<div class="expandable">
<h1>sub-foo2</h1>
<div class="expandable">
Stuff
</div>
</div>
</div>
<div class="expandable">
<h1>bar2</h1>
</div>
</div>
<script type="text/javascript">
$(document).ready(function () {
$('.expandable').hide().filter('.top').show(); //hide every expandable div except the top one
$('.expandable h1').click(function () {
$(this).hide(); //hides the header
$(this).parent().siblings().hide(); //hide the whole tier
//show the hidden siblings, then add a link to go back up
$(this).siblings().show().last().after('<a href="#" class="up-link">Up one level</a>');
//add a click handler to the new link to reverse what we've just done
$(this).siblings('a.up-link').click(function () {
$(this).siblings().hide().filter('h1').show();
$(this).parent().siblings(':not(h1)').show();
$(this).remove();
return false; //prevent event propagation on the link so you don't navigate to #
});
});
});
</script>