Last active
December 28, 2015 15:29
-
-
Save macu/7522140 to your computer and use it in GitHub Desktop.
Add "active" to elements defining ownp attribute specifying a selector matching a parent element.
This was written specifically to add "active" to nav elements for the current page in Bootstrap-based templates.
This file contains hidden or 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
| $(function(){ | |
| // loop all elements that specify an ownp attribute, | |
| // and activate those in a parent matching the specified selector. | |
| $("[ownp]").each(function(){ | |
| if ($(this).closest($(this).attr("ownp")).length) { | |
| $(this).addClass("active"); | |
| } | |
| }); | |
| }); |
This file contains hidden or 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
| <body id="homePage"> | |
| <ul class="nav navbar-nav"> | |
| <li ownp="#homePage"><a href="{{ site.baseurl }}/">Home</a></li> | |
| <li ownp="#aboutPage"><a href="{{ site.baseurl }}/about/">About</a></li> | |
| <li ownp="#resourcePage"><a href="{{ site.baseurl }}/resources/">Resources</a></li> | |
| <li ownp="#eventsPage"><a href="{{ site.baseurl }}/events/">Events & Workshops</a></li> | |
| <li ownp="#contactPage"><a href="{{ site.baseurl }}/contact/">Contact</a></li> | |
| <li ownp="#faqPage"><a href="{{ site.baseurl }}/faq/">FAQ</a></li> | |
| </ul> | |
| </body> |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
better to be more general -- why require the selector match a parent rather than any other single element in the page