mardi 24 mars 2015

JavaScript if/else not acting as expected

I'm using jQuery to select a button on my website and add an event listener to add a night-time class when it is clicked.


Initially the button works, however when I click the button again it won't run the code to remove the night-time class.


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 isn't working, but I feel like I'm missing something painfully obvious. Also, the .animate({}, 200) isn't working either, as it just instantly applies the class, but this problem isn't as important to me as the main one.


Aucun commentaire:

Enregistrer un commentaire