Reputation: 33
I've got an accordion style menu, what I'm trying to do is to remove the open
class when anywhere inside the body tag is clicked so it collapses. I've got the code but doesn't work, get the following error in Chrome:
Uncaught TypeError: Object has no method 'hasClass'
Code:
var toggleMainNav = function ($mainNav) {
var isOpen = $mainNav.hasClass('open');
if (isOpen) {
$('body').unbind('click');
$mainNav.removeClass('open');
} else {
$('body').bind('click', toggleMainNav);
$mainNav.addClass('open');
}
};
Upvotes: 0
Views: 331
Reputation: 1758
You seem to be using a DOM element while you want to use a jQuery object.
Wrap your variable in jQuery style :
$($mainNav).hasClass("open");
Upvotes: 0
Reputation: 10694
Use toggleClass
$("body").click(function(){
$mainNav.toggleClass("open");
});
Upvotes: 1