Last active
December 20, 2015 14:59
-
-
Save jrthib/6150941 to your computer and use it in GitHub Desktop.
Setting the body class to whichever element is clicked
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
| $('.nav li a').click(function() { | |
| // remove the extra classes before adding the current clicked class | |
| $('.nav li a').each(function(i, el) { // iterate through all of the menu items | |
| // get the current elements class | |
| var removeClass = $(el).attr('class'); | |
| // remove it from the body, if it has the class. | |
| if($('body').hasClass(removeClass)) $('body').removeClass(removeClass); | |
| }); | |
| // get the class of the element that was clicked | |
| var classFromClickedNav = $(this).attr('class'); | |
| // adds the class to the body tag. | |
| $('body').addClass(classFromClickedNav); | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment