Answer the question
In order to leave comments, you need to log in
How to add and remove a class from the body when clicking on an element?
How can I use jQuery to add a class to the body when clicking on a certain element (block, button or link), and remove it when clicked again?
I found one script here on the Toaster:
(function() {
var $body = document.body,
$nav - icon = $body.getElementsByClassName('nav-icon')[0];
if (typeof $nav - icon !== 'undefined') {
$nav - icon.addEventListener('click', function() {
$body.className = ($body.className == 'nav-active') ? '' : 'nav-active';
});
}
}).call(this);
$nav - icon = $body.getElementsByClassName('nav-icon')[0];
gives an error: "Uncaught SyntaxError: Unexpected token -".
Answer the question
In order to leave comments, you need to log in
$("div").click(function(){
$("body").toggleClass("class");
});
late)
$("block").click(function(event) {
$("body").toggleClass('someclass');
});
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question