Reputation: 57
I have a bit of javascript that finds a link on the page and if it's the same link as the current page it changes the css to current. This works fine. But what I want to do is extend this so for instance lets same the link is mysite.com/blog/post. I want it to go up a directory i.e. mysite.com/blog find if that links on the page and if it is highlight it, hopefully that makes sense.The code I have is:
<script type="text/javascript">
$(document).ready( function () {
var path = location.pathname;
if ( path )
$('#sidebar ul.archive a[href$="' + path + '"]').attr('class', 'current');
});
</script>
So I'm thinking I just need to change the var path to find the link to the directory above, but can't figure out how I'd do that?
Upvotes: 0
Views: 436
Reputation: 193281
If I understood you correctly..
$('#sidebar ul.archive a').filter(function() {
return path.match($(this).attr('href'));
}).addClass('current');
For example, this will add class current
to the link <a href="/blog">Blog</a>
if the current page is mysite.com/blog/post
.
Upvotes: 0
Reputation: 10139
You could try parsing the location.pathname
to remove the rightmost directory. Eg:
var path = location.pathname;
var parentPath = path.substr(0,path.lastIndexOf('/'));
Upvotes: 1