const toggleClass = (element, className, force) => { element.classList.toggle(className, force); }; const div = document.querySelector("div"); // Toggle the class "active" on the div toggleClass(div, "active"); // Force add the class "highlight" toggleClass(div, "highlight", true); // Force remove the class "hidden" toggleClass(div, "hidden", false); const box = document.querySelector(".box"); const button = document.getElementById("toggleBtn"); button.addEventListener("click", () => { toggleClass(box, "active"); }); const toggleSpan = (currentTarget, force = true) => { const compareBlock = currentTarget.closest(".compare-block"); if (!compareBlock) return; const currentSpan = compareBlock.querySelector("span"); if (currentSpan) { toggleClass(currentSpan, "show", force); if (currentSpan.className.indexOf("show") > -1) { currentSpan.setAttribute("aria-hidden", "false"); } else { currentSpan.setAttribute("aria-hidden", "true"); } } };
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter