Reputation: 129
so Im building a script where it will hide divs based upon which ID is in the url. This is what I have so far:
<script>
$(function(){
if (document.location.href.indexOf('#aberdeen') > 0)
$("#centralia, #chewelah, #everett, #harbour-pointe").hide();
if (document.location.href.indexOf('#centralia') > 0)
$("#aberdeen, #chewelah, #everett, #harbour-pointe").hide();
if (document.location.href.indexOf('#chewelah') > 0)
$("#aberdeen, #centralia, #everett, #harbour-pointe").hide();
if (document.location.href.indexOf('#everett') > 0)
$("#aberdeen, #chewelah, #centralia, #harbour-pointe").hide();
if (document.location.href.indexOf('#harbour-pointe') > 0)
$("#aberdeen, #chewelah, #everett, #centralia").hide();
if (document.location.href.indexOf('#') < 0)
$(".directory").show();
});
</script>
However it is still showing all of them even with an id appended in the URL. Any ideas?
Upvotes: 1
Views: 115
Reputation: 15982
As an alternative to Javascript solutions given above, you can do this in CSS (modern browsers only):
If you give all your divs for example a hashdisplay
class, you can have the following styles:
.hashdisplay { display: none; }
.hashdisplay:target { display: block; }
WARNING!! Only works in browsers that support the :target
pseudo-class!
http://reference.sitepoint.com/css/pseudoclass-target
Upvotes: 0
Reputation: 144689
try this, you can add a class to the items and use document.location.hash
:
$(function(){
var id = document.location.hash;
$('.all').not(id).hide() // hide them expect the element that has an id of hash
});
Upvotes: 2
Reputation: 227270
That's called a "hash", not an "ID". It's located in document.location.hash
, not document.location.href
.
if (document.location.hash === '#aberdeen') {
$("#centralia, #chewelah, #everett, #harbour-pointe").hide();
}
Upvotes: 7
Reputation: 252
Try :
if (document.location.hash.indexOf('#aberdeen') != -1) {
$("#centralia, #chewelah, #everett, #harbour-pointe").hide();
}
Upvotes: 0