Remove active class from toggled element on other element click

Multi tool use
Multi tool use











up vote
0
down vote

favorite












In the menu that you see in the jsfiddle https://jsfiddle.net/Adyyda/osnq30bg/, if i click on triangle from Item 2 (that has childrens), the childrens show.



If i click on triangle from Item 3, the childrens show.



The problem is that i do not manage to close the childrens from Item 1 which are already active. Tried all sort of solutions regarding toggle but in this particular case i miss something to make it happen?



<header class="main" id="siteheader">
<a href="#menu" class="menu-link active"><span></span> Menu</a>
<nav id="menu" class="menu">
<ul class="level-1">
<li><a href="">Item 1</a></li>
<li><a href="">Item 2</a><span class="has-subnav">▼</span>
<ul class="wide level-2">
<li><a href="">Child Item 1</a></li>
<li><a href="">Child Item 2</a></li>
<li><a href="">Child Item 3</a></li>
</ul>
</li>
<li><a href="">Item 3</a><span class="has-subnav">▼</span>
<ul class="level-2">
<li><a href="">Child Item 1</a></li>
<li><a href="">Child Item 2</a></li>
</ul>
</li>
<li><a href="">Item 4</a></li>
<li><a href="">Item 5</a></li>
</ul>
</nav>
</header>

// Multi-Toggle Navigation
$(function() {
$('body').addClass('js');
var $menu = $('#menu'),
$menulink = $('.menu-link'),
$menuTrigger = $('.has-subnav');

$menulink.click(function(e) {
e.preventDefault();
$menulink.toggleClass('active');
$menu.toggleClass('active');
});

$menuTrigger.click(function(e) {
e.preventDefault();
var $this = $(this);
$this.toggleClass('active').next('ul').toggleClass('active');
});

});

// Remove "Active" Class from Menu on Resize
$(window).resize(function() {
var viewportWidth = $(window).width();
if (viewportWidth > 925) {
$("#menu").removeClass("active");
}
});


a.menu-link {
color: #000;
display: block;
text-decoration: none;
}

.menu-link span {
border-bottom: solid 3px #000;
border-top: double 10px #000;
display: inline-block;
height: 4px;
margin: 0 5px -3px 0;
width: 30px;
}

.menu-link:hover span { border-color: #666; }

.menu, .menu > ul, .menu > ul ul {
clear: both;
display: flex;
flex-flow: column;
margin: 0;
}

.menu.active {
background: #f9f9f9;
border-bottom: 1px solid #d8d8d8;
border-top: 1px solid #d8d8d8;
margin: 1em 0 1em -12px;
max-height: 55em;
width: 100vw;
}

.js .menu > ul ul.active {
margin: 0;
max-height: 55em;
padding: 0;
}

.menu > ul { padding: 0; }

nav li {
display: inline-block;
margin: 0;
position: relative;
}

.menu li a {
color: #000;
display: inline-block;
font-size: 1.04em;
letter-spacing: .05em;
line-height: 2.5em;
text-decoration: none;
}

span.has-subnav {
display: block;
font-size: 1em;
line-height: 2.5em;
position: absolute;
right: 20px;
padding: 0 0.5em;
top: 0;
}
@media screen and (max-width:800px) {

.menu, .menu > ul ul {
margin: 0;
max-height: 0;
overflow: hidden;
}

.menu li a {
border-bottom: 1px solid #d8d8d8;
display: block;
padding-left: 15px;
}

.menu li li a { padding-left: 50px; }

.menu li:last-child a { border: none; }

.menu li li:last-child a { border-bottom: 1px solid #d8d8d8; }

.menu li:hover { background: #EDEDED; }
}
@media screen and (min-width: 801px) {

nav {
border-top: 3px solid #00578b;
border-bottom: 1px solid #a4d05e;
}

a.menu-link { display: none; }

.js .menu, .js .menu > ul ul {
max-height: none;
overflow: visible;
}

.js .menu > ul li:hover > ul { display: flex; }

.menu ul {
display: flex;
flex-flow: row;
height: 44px;
justify-content: space-between;
margin: 0;
padding: 0;
}

.menu span.has-subnav { display: none; }

.menu li a:hover { color: rgb(164,208,94); }

.menu li li a:hover {
background: rgba(164,208,94,0.1);
color: #000;
}

.menu ul ul {
background: #fff;
border: solid 1px rgba(164,208,94,1);
border-radius: 2px 2px 5px 5px;
border-top: solid 2px transparent;
display: none;
height: auto;
overflow: hidden;
padding: 0;
position: absolute;
text-align: left;
top: 43px;
width: 150px;
z-index: 999;
}

.chrome .js .menu > ul ul { top: 43px; }

.menu ul ul.wide { width: 300px; }

.menu ul ul li {
border-bottom: solid 1px rgba(164,208,94,0.5);
display: inline-block;
position: relative;
}

.menu > ul ul li:last-child { border-bottom: none; }

.menu ul ul li a {
display: block;
padding-left: 10px;
}
}









share|improve this question


























    up vote
    0
    down vote

    favorite












    In the menu that you see in the jsfiddle https://jsfiddle.net/Adyyda/osnq30bg/, if i click on triangle from Item 2 (that has childrens), the childrens show.



    If i click on triangle from Item 3, the childrens show.



    The problem is that i do not manage to close the childrens from Item 1 which are already active. Tried all sort of solutions regarding toggle but in this particular case i miss something to make it happen?



    <header class="main" id="siteheader">
    <a href="#menu" class="menu-link active"><span></span> Menu</a>
    <nav id="menu" class="menu">
    <ul class="level-1">
    <li><a href="">Item 1</a></li>
    <li><a href="">Item 2</a><span class="has-subnav">▼</span>
    <ul class="wide level-2">
    <li><a href="">Child Item 1</a></li>
    <li><a href="">Child Item 2</a></li>
    <li><a href="">Child Item 3</a></li>
    </ul>
    </li>
    <li><a href="">Item 3</a><span class="has-subnav">▼</span>
    <ul class="level-2">
    <li><a href="">Child Item 1</a></li>
    <li><a href="">Child Item 2</a></li>
    </ul>
    </li>
    <li><a href="">Item 4</a></li>
    <li><a href="">Item 5</a></li>
    </ul>
    </nav>
    </header>

    // Multi-Toggle Navigation
    $(function() {
    $('body').addClass('js');
    var $menu = $('#menu'),
    $menulink = $('.menu-link'),
    $menuTrigger = $('.has-subnav');

    $menulink.click(function(e) {
    e.preventDefault();
    $menulink.toggleClass('active');
    $menu.toggleClass('active');
    });

    $menuTrigger.click(function(e) {
    e.preventDefault();
    var $this = $(this);
    $this.toggleClass('active').next('ul').toggleClass('active');
    });

    });

    // Remove "Active" Class from Menu on Resize
    $(window).resize(function() {
    var viewportWidth = $(window).width();
    if (viewportWidth > 925) {
    $("#menu").removeClass("active");
    }
    });


    a.menu-link {
    color: #000;
    display: block;
    text-decoration: none;
    }

    .menu-link span {
    border-bottom: solid 3px #000;
    border-top: double 10px #000;
    display: inline-block;
    height: 4px;
    margin: 0 5px -3px 0;
    width: 30px;
    }

    .menu-link:hover span { border-color: #666; }

    .menu, .menu > ul, .menu > ul ul {
    clear: both;
    display: flex;
    flex-flow: column;
    margin: 0;
    }

    .menu.active {
    background: #f9f9f9;
    border-bottom: 1px solid #d8d8d8;
    border-top: 1px solid #d8d8d8;
    margin: 1em 0 1em -12px;
    max-height: 55em;
    width: 100vw;
    }

    .js .menu > ul ul.active {
    margin: 0;
    max-height: 55em;
    padding: 0;
    }

    .menu > ul { padding: 0; }

    nav li {
    display: inline-block;
    margin: 0;
    position: relative;
    }

    .menu li a {
    color: #000;
    display: inline-block;
    font-size: 1.04em;
    letter-spacing: .05em;
    line-height: 2.5em;
    text-decoration: none;
    }

    span.has-subnav {
    display: block;
    font-size: 1em;
    line-height: 2.5em;
    position: absolute;
    right: 20px;
    padding: 0 0.5em;
    top: 0;
    }
    @media screen and (max-width:800px) {

    .menu, .menu > ul ul {
    margin: 0;
    max-height: 0;
    overflow: hidden;
    }

    .menu li a {
    border-bottom: 1px solid #d8d8d8;
    display: block;
    padding-left: 15px;
    }

    .menu li li a { padding-left: 50px; }

    .menu li:last-child a { border: none; }

    .menu li li:last-child a { border-bottom: 1px solid #d8d8d8; }

    .menu li:hover { background: #EDEDED; }
    }
    @media screen and (min-width: 801px) {

    nav {
    border-top: 3px solid #00578b;
    border-bottom: 1px solid #a4d05e;
    }

    a.menu-link { display: none; }

    .js .menu, .js .menu > ul ul {
    max-height: none;
    overflow: visible;
    }

    .js .menu > ul li:hover > ul { display: flex; }

    .menu ul {
    display: flex;
    flex-flow: row;
    height: 44px;
    justify-content: space-between;
    margin: 0;
    padding: 0;
    }

    .menu span.has-subnav { display: none; }

    .menu li a:hover { color: rgb(164,208,94); }

    .menu li li a:hover {
    background: rgba(164,208,94,0.1);
    color: #000;
    }

    .menu ul ul {
    background: #fff;
    border: solid 1px rgba(164,208,94,1);
    border-radius: 2px 2px 5px 5px;
    border-top: solid 2px transparent;
    display: none;
    height: auto;
    overflow: hidden;
    padding: 0;
    position: absolute;
    text-align: left;
    top: 43px;
    width: 150px;
    z-index: 999;
    }

    .chrome .js .menu > ul ul { top: 43px; }

    .menu ul ul.wide { width: 300px; }

    .menu ul ul li {
    border-bottom: solid 1px rgba(164,208,94,0.5);
    display: inline-block;
    position: relative;
    }

    .menu > ul ul li:last-child { border-bottom: none; }

    .menu ul ul li a {
    display: block;
    padding-left: 10px;
    }
    }









    share|improve this question
























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      In the menu that you see in the jsfiddle https://jsfiddle.net/Adyyda/osnq30bg/, if i click on triangle from Item 2 (that has childrens), the childrens show.



      If i click on triangle from Item 3, the childrens show.



      The problem is that i do not manage to close the childrens from Item 1 which are already active. Tried all sort of solutions regarding toggle but in this particular case i miss something to make it happen?



      <header class="main" id="siteheader">
      <a href="#menu" class="menu-link active"><span></span> Menu</a>
      <nav id="menu" class="menu">
      <ul class="level-1">
      <li><a href="">Item 1</a></li>
      <li><a href="">Item 2</a><span class="has-subnav">▼</span>
      <ul class="wide level-2">
      <li><a href="">Child Item 1</a></li>
      <li><a href="">Child Item 2</a></li>
      <li><a href="">Child Item 3</a></li>
      </ul>
      </li>
      <li><a href="">Item 3</a><span class="has-subnav">▼</span>
      <ul class="level-2">
      <li><a href="">Child Item 1</a></li>
      <li><a href="">Child Item 2</a></li>
      </ul>
      </li>
      <li><a href="">Item 4</a></li>
      <li><a href="">Item 5</a></li>
      </ul>
      </nav>
      </header>

      // Multi-Toggle Navigation
      $(function() {
      $('body').addClass('js');
      var $menu = $('#menu'),
      $menulink = $('.menu-link'),
      $menuTrigger = $('.has-subnav');

      $menulink.click(function(e) {
      e.preventDefault();
      $menulink.toggleClass('active');
      $menu.toggleClass('active');
      });

      $menuTrigger.click(function(e) {
      e.preventDefault();
      var $this = $(this);
      $this.toggleClass('active').next('ul').toggleClass('active');
      });

      });

      // Remove "Active" Class from Menu on Resize
      $(window).resize(function() {
      var viewportWidth = $(window).width();
      if (viewportWidth > 925) {
      $("#menu").removeClass("active");
      }
      });


      a.menu-link {
      color: #000;
      display: block;
      text-decoration: none;
      }

      .menu-link span {
      border-bottom: solid 3px #000;
      border-top: double 10px #000;
      display: inline-block;
      height: 4px;
      margin: 0 5px -3px 0;
      width: 30px;
      }

      .menu-link:hover span { border-color: #666; }

      .menu, .menu > ul, .menu > ul ul {
      clear: both;
      display: flex;
      flex-flow: column;
      margin: 0;
      }

      .menu.active {
      background: #f9f9f9;
      border-bottom: 1px solid #d8d8d8;
      border-top: 1px solid #d8d8d8;
      margin: 1em 0 1em -12px;
      max-height: 55em;
      width: 100vw;
      }

      .js .menu > ul ul.active {
      margin: 0;
      max-height: 55em;
      padding: 0;
      }

      .menu > ul { padding: 0; }

      nav li {
      display: inline-block;
      margin: 0;
      position: relative;
      }

      .menu li a {
      color: #000;
      display: inline-block;
      font-size: 1.04em;
      letter-spacing: .05em;
      line-height: 2.5em;
      text-decoration: none;
      }

      span.has-subnav {
      display: block;
      font-size: 1em;
      line-height: 2.5em;
      position: absolute;
      right: 20px;
      padding: 0 0.5em;
      top: 0;
      }
      @media screen and (max-width:800px) {

      .menu, .menu > ul ul {
      margin: 0;
      max-height: 0;
      overflow: hidden;
      }

      .menu li a {
      border-bottom: 1px solid #d8d8d8;
      display: block;
      padding-left: 15px;
      }

      .menu li li a { padding-left: 50px; }

      .menu li:last-child a { border: none; }

      .menu li li:last-child a { border-bottom: 1px solid #d8d8d8; }

      .menu li:hover { background: #EDEDED; }
      }
      @media screen and (min-width: 801px) {

      nav {
      border-top: 3px solid #00578b;
      border-bottom: 1px solid #a4d05e;
      }

      a.menu-link { display: none; }

      .js .menu, .js .menu > ul ul {
      max-height: none;
      overflow: visible;
      }

      .js .menu > ul li:hover > ul { display: flex; }

      .menu ul {
      display: flex;
      flex-flow: row;
      height: 44px;
      justify-content: space-between;
      margin: 0;
      padding: 0;
      }

      .menu span.has-subnav { display: none; }

      .menu li a:hover { color: rgb(164,208,94); }

      .menu li li a:hover {
      background: rgba(164,208,94,0.1);
      color: #000;
      }

      .menu ul ul {
      background: #fff;
      border: solid 1px rgba(164,208,94,1);
      border-radius: 2px 2px 5px 5px;
      border-top: solid 2px transparent;
      display: none;
      height: auto;
      overflow: hidden;
      padding: 0;
      position: absolute;
      text-align: left;
      top: 43px;
      width: 150px;
      z-index: 999;
      }

      .chrome .js .menu > ul ul { top: 43px; }

      .menu ul ul.wide { width: 300px; }

      .menu ul ul li {
      border-bottom: solid 1px rgba(164,208,94,0.5);
      display: inline-block;
      position: relative;
      }

      .menu > ul ul li:last-child { border-bottom: none; }

      .menu ul ul li a {
      display: block;
      padding-left: 10px;
      }
      }









      share|improve this question













      In the menu that you see in the jsfiddle https://jsfiddle.net/Adyyda/osnq30bg/, if i click on triangle from Item 2 (that has childrens), the childrens show.



      If i click on triangle from Item 3, the childrens show.



      The problem is that i do not manage to close the childrens from Item 1 which are already active. Tried all sort of solutions regarding toggle but in this particular case i miss something to make it happen?



      <header class="main" id="siteheader">
      <a href="#menu" class="menu-link active"><span></span> Menu</a>
      <nav id="menu" class="menu">
      <ul class="level-1">
      <li><a href="">Item 1</a></li>
      <li><a href="">Item 2</a><span class="has-subnav">▼</span>
      <ul class="wide level-2">
      <li><a href="">Child Item 1</a></li>
      <li><a href="">Child Item 2</a></li>
      <li><a href="">Child Item 3</a></li>
      </ul>
      </li>
      <li><a href="">Item 3</a><span class="has-subnav">▼</span>
      <ul class="level-2">
      <li><a href="">Child Item 1</a></li>
      <li><a href="">Child Item 2</a></li>
      </ul>
      </li>
      <li><a href="">Item 4</a></li>
      <li><a href="">Item 5</a></li>
      </ul>
      </nav>
      </header>

      // Multi-Toggle Navigation
      $(function() {
      $('body').addClass('js');
      var $menu = $('#menu'),
      $menulink = $('.menu-link'),
      $menuTrigger = $('.has-subnav');

      $menulink.click(function(e) {
      e.preventDefault();
      $menulink.toggleClass('active');
      $menu.toggleClass('active');
      });

      $menuTrigger.click(function(e) {
      e.preventDefault();
      var $this = $(this);
      $this.toggleClass('active').next('ul').toggleClass('active');
      });

      });

      // Remove "Active" Class from Menu on Resize
      $(window).resize(function() {
      var viewportWidth = $(window).width();
      if (viewportWidth > 925) {
      $("#menu").removeClass("active");
      }
      });


      a.menu-link {
      color: #000;
      display: block;
      text-decoration: none;
      }

      .menu-link span {
      border-bottom: solid 3px #000;
      border-top: double 10px #000;
      display: inline-block;
      height: 4px;
      margin: 0 5px -3px 0;
      width: 30px;
      }

      .menu-link:hover span { border-color: #666; }

      .menu, .menu > ul, .menu > ul ul {
      clear: both;
      display: flex;
      flex-flow: column;
      margin: 0;
      }

      .menu.active {
      background: #f9f9f9;
      border-bottom: 1px solid #d8d8d8;
      border-top: 1px solid #d8d8d8;
      margin: 1em 0 1em -12px;
      max-height: 55em;
      width: 100vw;
      }

      .js .menu > ul ul.active {
      margin: 0;
      max-height: 55em;
      padding: 0;
      }

      .menu > ul { padding: 0; }

      nav li {
      display: inline-block;
      margin: 0;
      position: relative;
      }

      .menu li a {
      color: #000;
      display: inline-block;
      font-size: 1.04em;
      letter-spacing: .05em;
      line-height: 2.5em;
      text-decoration: none;
      }

      span.has-subnav {
      display: block;
      font-size: 1em;
      line-height: 2.5em;
      position: absolute;
      right: 20px;
      padding: 0 0.5em;
      top: 0;
      }
      @media screen and (max-width:800px) {

      .menu, .menu > ul ul {
      margin: 0;
      max-height: 0;
      overflow: hidden;
      }

      .menu li a {
      border-bottom: 1px solid #d8d8d8;
      display: block;
      padding-left: 15px;
      }

      .menu li li a { padding-left: 50px; }

      .menu li:last-child a { border: none; }

      .menu li li:last-child a { border-bottom: 1px solid #d8d8d8; }

      .menu li:hover { background: #EDEDED; }
      }
      @media screen and (min-width: 801px) {

      nav {
      border-top: 3px solid #00578b;
      border-bottom: 1px solid #a4d05e;
      }

      a.menu-link { display: none; }

      .js .menu, .js .menu > ul ul {
      max-height: none;
      overflow: visible;
      }

      .js .menu > ul li:hover > ul { display: flex; }

      .menu ul {
      display: flex;
      flex-flow: row;
      height: 44px;
      justify-content: space-between;
      margin: 0;
      padding: 0;
      }

      .menu span.has-subnav { display: none; }

      .menu li a:hover { color: rgb(164,208,94); }

      .menu li li a:hover {
      background: rgba(164,208,94,0.1);
      color: #000;
      }

      .menu ul ul {
      background: #fff;
      border: solid 1px rgba(164,208,94,1);
      border-radius: 2px 2px 5px 5px;
      border-top: solid 2px transparent;
      display: none;
      height: auto;
      overflow: hidden;
      padding: 0;
      position: absolute;
      text-align: left;
      top: 43px;
      width: 150px;
      z-index: 999;
      }

      .chrome .js .menu > ul ul { top: 43px; }

      .menu ul ul.wide { width: 300px; }

      .menu ul ul li {
      border-bottom: solid 1px rgba(164,208,94,0.5);
      display: inline-block;
      position: relative;
      }

      .menu > ul ul li:last-child { border-bottom: none; }

      .menu ul ul li a {
      display: block;
      padding-left: 10px;
      }
      }






      jquery






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked 20 mins ago









      Adyyda

      13612




      13612
























          1 Answer
          1






          active

          oldest

          votes

















          up vote
          0
          down vote













          There is nowhere in your $menuTrigger.click function where you "tell" others dropdowns to close, actually. You can do it by closing each opened one before opening the clicked one:



          $menuTrigger.click(function(e) {
          e.preventDefault();
          $menuTrigger.each(function(){
          if($(this).hasClass('active')){
          $(this).removeClass('active').next('ul').removeClass('active');
          }
          });
          var $this = $(this);
          $this.toggleClass('active').next('ul').toggleClass('active');
          });


          see forked updated fiddle here





          share





















            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














             

            draft saved


            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53204464%2fremove-active-class-from-toggled-element-on-other-element-click%23new-answer', 'question_page');
            }
            );

            Post as a guest
































            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            0
            down vote













            There is nowhere in your $menuTrigger.click function where you "tell" others dropdowns to close, actually. You can do it by closing each opened one before opening the clicked one:



            $menuTrigger.click(function(e) {
            e.preventDefault();
            $menuTrigger.each(function(){
            if($(this).hasClass('active')){
            $(this).removeClass('active').next('ul').removeClass('active');
            }
            });
            var $this = $(this);
            $this.toggleClass('active').next('ul').toggleClass('active');
            });


            see forked updated fiddle here





            share

























              up vote
              0
              down vote













              There is nowhere in your $menuTrigger.click function where you "tell" others dropdowns to close, actually. You can do it by closing each opened one before opening the clicked one:



              $menuTrigger.click(function(e) {
              e.preventDefault();
              $menuTrigger.each(function(){
              if($(this).hasClass('active')){
              $(this).removeClass('active').next('ul').removeClass('active');
              }
              });
              var $this = $(this);
              $this.toggleClass('active').next('ul').toggleClass('active');
              });


              see forked updated fiddle here





              share























                up vote
                0
                down vote










                up vote
                0
                down vote









                There is nowhere in your $menuTrigger.click function where you "tell" others dropdowns to close, actually. You can do it by closing each opened one before opening the clicked one:



                $menuTrigger.click(function(e) {
                e.preventDefault();
                $menuTrigger.each(function(){
                if($(this).hasClass('active')){
                $(this).removeClass('active').next('ul').removeClass('active');
                }
                });
                var $this = $(this);
                $this.toggleClass('active').next('ul').toggleClass('active');
                });


                see forked updated fiddle here





                share












                There is nowhere in your $menuTrigger.click function where you "tell" others dropdowns to close, actually. You can do it by closing each opened one before opening the clicked one:



                $menuTrigger.click(function(e) {
                e.preventDefault();
                $menuTrigger.each(function(){
                if($(this).hasClass('active')){
                $(this).removeClass('active').next('ul').removeClass('active');
                }
                });
                var $this = $(this);
                $this.toggleClass('active').next('ul').toggleClass('active');
                });


                see forked updated fiddle here






                share











                share


                share










                answered 4 mins ago









                Kaddath

                2,2301315




                2,2301315






























                     

                    draft saved


                    draft discarded



















































                     


                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53204464%2fremove-active-class-from-toggled-element-on-other-element-click%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest




















































































                    ZDFRsBJ5YcGqVH Gi
                    KZ1cO15gZTwZXj,XU kKuqFPlTFUvGmHT

                    Popular posts from this blog

                    How to pass form data using jquery Ajax to insert data in database?

                    Guess what letter conforming each word

                    Run scheduled task as local user group (not BUILTIN)