Gir
Gir

Reputation: 35

Drop down menu with clicks

I am creating a drop down menu that shows a submenu when clicked instead of using hover.

When I click it is displayed as it should be but I would like that when I click on another option the previous one is hidden and not kept open as right now.

In advance, thank you very much to the person who takes the trouble to help me, here is the code I'm working with.

$('.parent a').on("click", function(e) {
  $(this).next('ul').toggle();
  e.stopPropagation();
  e.preventDefault();
});
* {
  margin: 0;
  padding: 0;
  outline: 0;
  box-sizing: border-box;
}

.parent {
  display: block;
  position: relative;
  float: left;
  line-height: 30px;
  background-color: #4FA0D8;
  border-right: #CCC 1px solid;
}

.parent a {
  margin: 10px;
  color: #FFFFFF;
  text-decoration: none;
}

.parent>ul {
  position: absolute;
}

.child {
  display: none;
}

.child li {
  background-color: #E4EFF7;
  line-height: 30px;
  border-bottom: #CCC 1px solid;
  border-right: #CCC 1px solid;
  width: 100%;
}

.child li a {
  color: #000000;
}

ul {
  list-style: none;
  margin: 0;
  padding: 0px;
  min-width: 10em;
}

ul ul ul {
  left: 100%;
  top: 0;
  margin-left: 1px;
}

li:hover {
  background-color: #95B4CA;
}

.parent li:hover {
  background-color: #F0F0F0;
}

.expand {
  font-size: 12px;
  float: right;
  margin-right: 5px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<ul id="menu">
  <li class="parent"><a href="#">Popular Toys</a>
    <ul class="child">
      <li class="parent"><a href="#">Video Games <span class="expand">»</span></a>
        <ul class="child">
          <li><a href="#">Car</a></li>
          <li><a href="#">Bike Race</a></li>
          <li><a href="#">Fishing</a></li>
        </ul>
      </li>
      <li><a href="#">Barbies</a></li>
      <li><a href="#">Teddy Bear</a></li>
      <li><a href="#">Golf Set</a></li>
    </ul>
  </li>
  <li class="parent"><a href="#">Recent Toys</a>
    <ul class="child">
      <li><a href="#">Yoyo</a></li>
      <li><a href="#">Doctor Kit</a></li>
      <li class="parent"><a href="#">Fun Puzzle<span class="expand">»</span></a>
        <ul class="child">
          <li><a href="#" nowrap>Cards</a></li>
          <li><a href="#" nowrap>Numbers</a></li>
        </ul>
      </li>
      <li><a href="#">Uno Cards</a></li>
    </ul>
  </li>
  <li class="parent"><a href="#">Toys Category</a>
    <ul class="child">
      <li><a href="#">Battery Toys</a></li>
      <li class="parent"><a href="#">Remote Toys <span class="expand">»</span></a>
        <ul class="child">
          <li><a href="#">Cars</a></li>
          <li><a href="#">Aeroplane</a></li>
          <li><a href="#">Helicopter</a></li>
        </ul>
      </li>
      <li><a href="#">Soft Toys</a>
      </li>
      <li><a href="#">Magnet Toys</a></li>
    </ul>
  </li>
</ul>

Upvotes: 2

Views: 150

Answers (3)

user3051780
user3051780

Reputation:

You could try with

$('.parent a').on("click", function (e) {
    $('.parent ul').hide();
    $(this).next('ul').toggle();
    e.stopPropagation();
    e.preventDefault();
});

Upvotes: 0

Frenchy
Frenchy

Reputation: 16997

using siblings(), you have quickest solution:

parents("li.parent").last() keeps the highest parent li.parent of selected item

.sibling() keeps all others li.parent brothers (of main menu)

.find("ul") keeps all ul children from all li.parent brothers

$('.parent a').on("click", function(e) {
  $(this).parents("li.parent").last().siblings().find("ul").hide();
  $(this).next('ul').toggle();

  e.stopPropagation();
  e.preventDefault();
});

$('.parent a').on("click", function(e) {
  $(this).parents("li.parent").last().siblings().find("ul").hide();
  $(this).next('ul').toggle();

  e.stopPropagation();
  e.preventDefault();
});
* {
  margin: 0;
  padding: 0;
  outline: 0;
  box-sizing: border-box;
}

.parent {
  display: block;
  position: relative;
  float: left;
  line-height: 30px;
  background-color: #4FA0D8;
  border-right: #CCC 1px solid;
}

.parent a {
  margin: 10px;
  color: #FFFFFF;
  text-decoration: none;
}

.parent>ul {
  position: absolute;
}

.child {
  display: none;
}

.child li {
  background-color: #E4EFF7;
  line-height: 30px;
  border-bottom: #CCC 1px solid;
  border-right: #CCC 1px solid;
  width: 100%;
}

.child li a {
  color: #000000;
}

ul {
  list-style: none;
  margin: 0;
  padding: 0px;
  min-width: 10em;
}

ul ul ul {
  left: 100%;
  top: 0;
  margin-left: 1px;
}

li:hover {
  background-color: #95B4CA;
}

.parent li:hover {
  background-color: #F0F0F0;
}

.expand {
  font-size: 12px;
  float: right;
  margin-right: 5px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<ul id="menu">
  <li class="parent"><a href="#">Popular Toys</a>
    <ul class="child">
      <li class="parent"><a href="#">Video Games <span class="expand">»</span></a>
        <ul class="child">
          <li><a href="#">Car</a></li>
          <li><a href="#">Bike Race</a></li>
          <li><a href="#">Fishing</a></li>
        </ul>
      </li>
      <li><a href="#">Barbies</a></li>
      <li><a href="#">Teddy Bear</a></li>
      <li><a href="#">Golf Set</a></li>
    </ul>
  </li>
  <li class="parent"><a href="#">Recent Toys</a>
    <ul class="child">
      <li><a href="#">Yoyo</a></li>
      <li><a href="#">Doctor Kit</a></li>
      <li class="parent"><a href="#">Fun Puzzle<span class="expand">»</span></a>
        <ul class="child">
          <li><a href="#" nowrap>Cards</a></li>
          <li><a href="#" nowrap>Numbers</a></li>
        </ul>
      </li>
      <li><a href="#">Uno Cards</a></li>
    </ul>
  </li>
  <li class="parent"><a href="#">Toys Category</a>
    <ul class="child">
      <li><a href="#">Battery Toys</a></li>
      <li class="parent"><a href="#">Remote Toys <span class="expand">»</span></a>
        <ul class="child">
          <li><a href="#">Cars</a></li>
          <li><a href="#">Aeroplane</a></li>
          <li><a href="#">Helicopter</a></li>
        </ul>
      </li>
      <li><a href="#">Soft Toys</a>
      </li>
      <li><a href="#">Magnet Toys</a></li>
    </ul>
  </li>
</ul>

Upvotes: 0

Adharsh M
Adharsh M

Reputation: 3822

Move the display logic to ".active" selector in css,

.active {
    display: block;
}

then try the following script.

It would check if the click is not inside current menu, if the click is anywhere but current menu, active class will be removed.

var menu = $('.parent a').next('ul')

$(document).mouseup(e => {
    if (!menu.is(e.target) // if the target of the click isn't the container...
        && menu.has(e.target).length === 0) // ... nor a descendant of the container
        {
            menu.removeClass("active");
        }
    });

$('.parent a').on("click", function (e) {
    $(this).next('ul').toggleClass("active");
    e.stopPropagation();
    e.preventDefault();
});

Here's the full code,

var menu = $('.parent a').next('ul')

$(document).mouseup(e => {
    if (!menu.is(e.target) // if the target of the click isn't the container...
        && menu.has(e.target).length === 0) // ... nor a descendant of the container
    {
        menu.removeClass("active");
    }
});

$('.parent a').on("click", function (e) {
    $(this).next('ul').toggleClass("active");
    e.stopPropagation();
    e.preventDefault();
});
* {
  margin: 0;
  padding: 0;
  outline: 0;
  box-sizing: border-box;
}

.parent {
  display: block;
  position: relative;
  float: left;
  line-height: 30px;
  background-color: #4FA0D8;
  border-right: #CCC 1px solid;
}

.parent a {
  margin: 10px;
  color: #FFFFFF;
  text-decoration: none;
}

.parent>ul {
  position: absolute;
}

.child {
  display: none;
}

.active {
    display: block;
}

.child li {
  background-color: #E4EFF7;
  line-height: 30px;
  border-bottom: #CCC 1px solid;
  border-right: #CCC 1px solid;
  width: 100%;
}

.child li a {
  color: #000000;
}

ul {
  list-style: none;
  margin: 0;
  padding: 0px;
  min-width: 10em;
}

ul ul ul {
  left: 100%;
  top: 0;
  margin-left: 1px;
}

li:hover {
  background-color: #95B4CA;
}

.parent li:hover {
  background-color: #F0F0F0;
}

.expand {
  font-size: 12px;
  float: right;
  margin-right: 5px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<ul id="menu">
  <li class="parent"><a href="#">Popular Toys</a>
    <ul class="child">
      <li class="parent"><a href="#">Video Games <span class="expand">»</span></a>
        <ul class="child">
          <li><a href="#">Car</a></li>
          <li><a href="#">Bike Race</a></li>
          <li><a href="#">Fishing</a></li>
        </ul>
      </li>
      <li><a href="#">Barbies</a></li>
      <li><a href="#">Teddy Bear</a></li>
      <li><a href="#">Golf Set</a></li>
    </ul>
  </li>
  <li class="parent"><a href="#">Recent Toys</a>
    <ul class="child">
      <li><a href="#">Yoyo</a></li>
      <li><a href="#">Doctor Kit</a></li>
      <li class="parent"><a href="#">Fun Puzzle<span class="expand">»</span></a>
        <ul class="child">
          <li><a href="#" nowrap>Cards</a></li>
          <li><a href="#" nowrap>Numbers</a></li>
        </ul>
      </li>
      <li><a href="#">Uno Cards</a></li>
    </ul>
  </li>
  <li class="parent"><a href="#">Toys Category</a>
    <ul class="child">
      <li><a href="#">Battery Toys</a></li>
      <li class="parent"><a href="#">Remote Toys <span class="expand">»</span></a>
        <ul class="child">
          <li><a href="#">Cars</a></li>
          <li><a href="#">Aeroplane</a></li>
          <li><a href="#">Helicopter</a></li>
        </ul>
      </li>
      <li><a href="#">Soft Toys</a>
      </li>
      <li><a href="#">Magnet Toys</a></li>
    </ul>
  </li>
</ul>

Upvotes: 2

Related Questions