I use jQuery to select a button on my website and add an event listener to add a class night-timewhen clicked.
The button initially works, however, when I click the button again, it will not run code to delete the class night-time.
Here is the JavaScript code:
var night_time = false;
if (!night_time) {
$('.blog-desc').find('a').on('click', function() {
$('html').addClass('night-time').animate(200);
night_time = true;
console.log("Making it night!");
});
} else {
$('.blog-desc').find('a').on('click', function() {
$('html').attr('class', "");
night_time = false;
console.log("Making it day!");
});
}
I really don't know why this does not work, but I feel that I am missing something that is painfully obvious. In addition, it .animate({}, 200)also does not work, since he immediately applies the class, but this problem is not as important for me as the main one.
source
share