jquery - Bind two selectors, one for addClass and the other on to removeClass -


as title says, how bind 2 selectors open/close (add , remove class) on div?

simple html:

<div> <a href="#" class="open">open</a> <div> <span class="close">x</span> </div> </div> 

some jquery:

$(function() {   var $open = $("a.open");   var $close = $("span.close");   $open.on("click", function() {     var $this = $(this);      if ($this.hasclass("active")) {       $this.removeclass("active");       $this.parent("div").removeclass("is-open");     } else {       $this.addclass("active");       $this.parent("div").addclass("is-open");     }   });    $close.on("click", function() {     // , close should remove active class div , remove active class "a".    }); }); 

once again; if click on "a" open div , press "span" close div. best approach?

you can use,

  $close.on("click", function() {     var container = $(this).parent().parent();     container.find(".active").removeclass("active");     container.removeclass("is-open");    }); 

Comments

Popular posts from this blog

scala - 'wrong top statement declaration' when using slick in IntelliJ -

c# - DevExpress.Wpf.Grid.InfiniteGridSizeException was unhandled -

PySide and Qt Properties: Connecting signals from Python to QML -