Hint: I used the functions removeClass() and addClass() to make things easier. You can find them on See the Pen Hide an element when clicking on somewhere else by Lars Gerrit Kliesing LGK (@lgkonline) on CodePen.
Tag: DOM
JavaScript “hasClass()”, “addClass()”, “removeClass()” helper functions
If you’re like me and come from jQuery to pure JavaScript, these helper functions might be very helpful for you: function hasClass(el, className) { if (el.classList) return el.classList.contains(className); else return !!el.className.match(new RegExp(‘(\\s|^)’ + className + ‘(\\s|$)’)); } function addClass(el, className) { if (el.classList) el.classList.add(className); else if (!hasClass(el, className)) el.className += ” ” + className; }… Continue reading JavaScript “hasClass()”, “addClass()”, “removeClass()” helper functions