If you had this markup:
<a href="#div5" class="toggler">Toggle Div 5</a> <div id="div5">Content for Div 5</div>
You can do it in jQuery:
$("a.toggler").click(function() { $(this.hash).slideToggle(); });
Or use rel or something with the div you are clicking on, for example:
<div rel="#div5" class="toggler">Toggle Div 5</a> <div id="div5">Content for Div 5</div>
And tune your jQuery to this:
$("div.toggler").click(function() { var hash = $(this).attr("rel"); $(hash).slideToggle(); window.location.hash = hash; });
My recommendation was to use display:block; anchored to do what you want and use the default browser behavior here, it takes care of the hash on click.
No matter which approach is above, you can show one on the page like this:
$(function() { if(location.hash != "") { $(location.hash + ":hidden").slideDown(); } });
source share