Created
April 12, 2013 22:41
-
-
Save daverogers/5375778 to your computer and use it in GitHub Desktop.
Assign "active" class to navbar item based on current page URL with jQuery
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!-- your average Bootstrap navbar component, in this case placed in an include called header.html --> | |
<div class="navbar"> | |
<div class="navbar-inner"> | |
<ul class="nav"> | |
<li><a href="/">Home</a></li> | |
<li><a href="/about.php">About</a></li> | |
<li><a href="/contact.php">Contact</a></li> | |
</ul> | |
</div> | |
</div> |
@ericauv I think because it's a variable, you should use a +
@ericauv the + here works as a text concatenation operator:
mytext
=
'.nav > li > a[href="'
+
pathname
+
'"]'
Please note the trick of using single quote for string that contains a double quote.
So, at the end of the day, if the variable pathname
as a value of www.mysite.com
then
mytext
=
.nav > li > a[href="www.mysite.com"]
Thanks for the help
Thank you very much! it's works
Thx! Simple and effective 👍
Works like a charm. Thanks man.
I have been looking for something like this FOR DAYS!! thank you from a rookie!
Good job...please how do I make each page of this code active
`
Thank you sir.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I can't find any information on what the "+" in
$('.nav > li > a[href="'+pathname+'"]').parent().addClass('active');
do... can you please explain