I have this code as an example:
<html>
<head>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js"></script>
<script type="text/javascript">
function showonlyone(thechosenone) {
$('.newboxes').each(function(index) {
if ($(this).attr("id") == thechosenone) {
$(this).show(200);
}
else {
$(this).hide(600);
}
});
}
</script>
</head>
<body>
<div style="border: 1px solid blue; background-color: #99CCFF; padding: 5px; width: 150px;">
<a id="myHeader1" href="javascript:showonlyone('newboxes1');" >show this one only</a>
</div>
<div class="newboxes" id="newboxes1" style="border: 1px solid black; background-color: #CCCCCC; display: block;padding: 5px; width: 150px;">Div #1</div>
<div style="border: 1px solid blue; background-color: #99CCFF; padding: 5px; width: 150px;">
<a id="myHeader2" href="javascript:showonlyone('newboxes2');" >show this one only</a>
</div>
<div class="newboxes" id="newboxes2" style="border: 1px solid black; background-color: #CCCCCC; display: none;padding: 5px; width: 150px;">Div #2</div>
<div style="border: 1px solid blue; background-color: #99CCFF; padding: 5px; width: 150px;">
<a id="myHeader3" href="javascript:showonlyone('newboxes3');" >show this one only</a>
</div>
<div class="newboxes" id="newboxes3" style="border: 1px solid black; background-color: #CCCCCC; display: none;padding: 5px; width: 150px;">Div #3</div>
</body>
</html>
On a different page, I want to have a link that will show Div #2 when the user loads the page by clicking that external link. I want this to be possible by adding a hash variable to the URL, for instance: domain.com/page.html#newboxes2
Someone told me that such feature is possible by playing around this with:
$(function(){
switch( window.location.hash ){
case '#showcontainer1':
$('#container1').fadeIn();
break;
default:
$('#container2').fadeIn()
break;
}
});
However, after trying to wrap my head around how to add that code to my own code, I could not make it work. How does this work?
Any assistance is appreciate, my knowledge of coding is very limited so please be detailed if you choose to help.
Using your current html structure, all you will need to do is this. Navigating to
domain.com/page.html#newboxes2will result in the<div>with id as#newboxes2will be shown. As you did before, set all of them todisplay:none;by default. You should consider pulling your styling out of the html and instead put them all between<style>tags at the top of your page. You can use class selectors (e.g..newboxes) to apply the same style to all elements of a particular class.Demo: http://jsfiddle.net/eqPGW/4/
Javascript:
HTML/CSS: