Answer the question
In order to leave comments, you need to log in
How to remove the class when clicking on the body?
I have a div which has an image. Clicking on this div adds a class (class with style flips this image by 180deg) and clicking on the div again removes this class.
I need the class to be removed when clicking outside this div.
Code:
(function (document) {
var div = document.getElementById('dropdownMenuButton');
var icon = document.getElementById('arrow_lang');
var open = false;
div.addEventListener('click', function () {
if (open) {
icon.className = 'arrow_lang';
} else {
icon.className = 'arrow_lang open';
}
open = !open;
});
})(document);
(function (document) {
var div = document.getElementById('dropdownMenuButton');
var icon = document.getElementById('arrow_lang');
var open = false;
div.addEventListener('click', function (e) {
e.preventDefault();
if (open) {
icon.className = 'arrow_lang';
} else {
icon.className = 'arrow_lang open';
}
open = !open;
});
document.body.addeventlistener('click', function() {
if (open) {
icon.className = 'arrow_lang';
open = !open;
}
});
})(document);
Answer the question
In order to leave comments, you need to log in
Try changing
document. body .addEventListener -> document.addEventListener
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question