Skip to content

Instantly share code, notes, and snippets.

@jrthib
Last active December 20, 2015 14:59
Show Gist options
  • Select an option

  • Save jrthib/6150941 to your computer and use it in GitHub Desktop.

Select an option

Save jrthib/6150941 to your computer and use it in GitHub Desktop.
Setting the body class to whichever element is clicked
$('.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