1

So I have this code:

$(document).click(function(e){
  $(".label").removeClass("active");
});

$(".input-combo").click(function(e){
  e.stopPropagation();

  var $this = $(this),
      label = $this.find(".label");

  $(".label").removeClass("active");
  label.addClass("active");
});

And apart from the $(document) click part it works ok, but with the document part the first click event cancels out the $(".input-combo") click event. How could I prevent this?

Xeen
  • 6,955
  • 16
  • 60
  • 111

2 Answers2

1

Following Code should work:

$(document:not(.input-combo)).click(function(e){
  $(".label").removeClass("active");
});

$(".input-combo").click(function(e){
  e.preventDefault();

  var $this = $(this),
      label = $this.find(".label");

  $(".label").removeClass("active");
  label.addClass("active");
});
Kumar_Vikas
  • 837
  • 7
  • 16
0

Use .not() selector in which you will exclude all the functions you don't want to remove.

DaX
  • 94
  • 1
  • 11