Skip to content

Instantly share code, notes, and snippets.

@treykane
Last active October 10, 2021 20:28
Show Gist options
  • Save treykane/db9378c0066fdb6938b0 to your computer and use it in GitHub Desktop.
Save treykane/db9378c0066fdb6938b0 to your computer and use it in GitHub Desktop.
Toggle a class, and remove the class when you click ANYWHERE ELSE on the document.
$( document ).ready(function() {
$(function() { // TOGGLE CLASS ON ELEMENT
$(".tile-top").on("click", function() {
$(this).toggleClass('active'); });
}); //END TOGGLE CLASS
$(function() { // REMOVE CLASS WHEN YOU CLICK, EXCLUDE ELEMENTS
var ignore= Array(".tile-top", // elements to ignore
".tile-bottom");
ignore.forEach(function (item) { // loop through ignore array
$(item).click(function(){ return false; }); // ignore item
});
$(document).on("click", function() { // remove class when you click anywhere else
$(".tile-top").removeClass('active'); });
});// END REMOVE CLASS
});
@Batareika007
Copy link

Thanks! working like charm

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment