lalit kumar
lalit kumar

Reputation: 163

Close the dropdown menu if the user clicks on the second dropdown link

I have a navigation bar with multiple drop downs. So when I click on the first link it opens the drop down, but when I click on the second link, the first drop down doesn't close. (so I want to close the drop down menu if the user clicks on second link)

// main.js (javascript file)

function myFunction() {
  var x = document.getElementById("myTopnav1");
  if (x.className === "topnav1") {
    x.className += " responsive1";
  } else {
    x.className = "topnav1";
  }
}

function toggleDropDown(myid) {
  var element = document.getElementById(myid);
  element.classList.toggle("mystyle");
}

// Close the dropdown menu if the user clicks outside of it
window.onclick = function(event) {
  if (!event.target.matches('.dropbtn1')) {

    var dropdowns = document.getElementsByClassName("dropdown-content1");
    var i;
    for (i = 0; i < dropdowns.length; i++) {
      var openDropdown = dropdowns[i];
      if (openDropdown.classList.contains('mystyle')) {
        openDropdown.classList.remove('mystyle');
      }
    }
  }
}
/* style.css (css file - css is all correct if anything you think is not added i only need help with javascript) */

.topnav1 {
  background-color: blue !important;
}

/* Style the links inside the navigation bar */

.topnav1 a {
  float: left;
  display: block;
  color: black;
  text-align: center;
  padding: 4px 8px;
  text-decoration: none;
  font-size: 10px;
  border-bottom: 1px solid #FDFDFD;
}

.topnav-right1 {
  float: right;
}

@media only screen and (max-width:768px) {
  .topnav-right1 {
    float: left;
  }
}

.para-active {
  background-color: #4F7ADA !important;
  color: black !important;
}

.para-active button {
  color: white !important;
}

/* Add an active class to highlight the current page */

.active1 {
  color: black !important;
}

/* Hide the link that should open and close the topnav on small screens */

.topnav1 .icon {
  display: none;
}

/* Dropdown container - needed to position the dropdown content */

.dropdown1 {
  float: left;
  overflow: hidden;
  background-color: #f1f1f1;
  border-bottom: 1px solid #E3E3E3;
}

/* Style the dropdown button to fit inside the topnav */

.dropdown1 .dropbtn1 {
  font-size: 10px;
  border: none;
  outline: none;
  color: black;
  padding: 4px 8px;
  background-color: inherit;
  font-family: inherit;
  margin: 0;
  border-bottom: 1px solid #FDFDFD;
}

/* Style the dropdown content (hidden by default) */

.dropdown-content1 {
  display: none;
  position: absolute;
  background-color: #f9f9f9;
  min-width: 96px;
  box-shadow: 0px 8px 16px 0px rgba(0, 0, 0, 0.2);
  z-index: 1;
}

.mystyle {
  display: block;
}
<div class="topnav1" id="myTopnav1">
  <div class="dropdown1">
    <button style="display: none;" id="btn_em" onclick="toggleDropDown('div_em')" class="dropbtn1">Meters 
                </button>
    <div class="dropdown-content1" id="div_em">
      <label class="dropnav-container">one</label>
      <label class="dropnav-container">one</label>
      <label class="dropnav-container">one</label>
    </div>
  </div>
  <div class="dropdown1">
    <button id="btn_inv" onclick="toggleDropDown('div_inv')" class="dropbtn1">Inverters 
                </button>
    <div class="dropdown-content1" id="div_inv">
      <label class="dropnav-container">two</label>
      <label class="dropnav-container">two</label>
      <label class="dropnav-container">two</label>
    </div>
  </div>
  <div class="dropdown1">
    <button id="btn_inm" onclick="toggleDropDown('div_inm')" class="dropbtn1">Inverter Manager 
                </button>
    <div class="dropdown-content1" id="div_inm">
      <label class="dropnav-container">three</label>
      <label class="dropnav-container">three</label>
      <label class="dropnav-container">three</label>
    </div>
  </div>
  <div class="dropdown1">
    <button id="btn_ws" onclick="toggleDropDown('div_ws')" class="dropbtn1">Sensors 
                </button>
    <div class="dropdown-content1" id="div_ws">
      <label class="dropnav-container">four</label>
      <label class="dropnav-container">four</label>
      <label class="dropnav-container">four</label>
    </div>
  </div>
  <div class="dropdown1">
    <button id="btn_smu" onclick="toggleDropDown('div_smu')" class="dropbtn1">SMUs 
                </button>
    <div class="dropdown-content1" id="div_smu">
      <label class="dropnav-container">five</label>
      <label class="dropnav-container">five</label>
      <label class="dropnav-container">five</label> </div>
  </div>

  <a href="javascript:void(0);" class="icon" onclick="myFunction()">&#9776;</a>
</div>

Upvotes: 1

Views: 967

Answers (3)

Zaffron
Zaffron

Reputation: 154

I assume that your mystyle class makes the dropdown active. Then you can do something like this

// Get all the dropdowns
var dropdowns = document.getElementByClassName(".dropdown1")

// When clicked on dropdown this function will fire
var callThisFunction = function (e) {
    // Check the event
    e = e || window.event;

    // Get the target element
    let target = e.target || e.srcElement;

    // Close all dropdowns
    let activeDropdowns = document.getElementsByClassName("mystyle");
    activeDropdowns.forEach(function (openDropdown) {
          openDropdown.classList.remove('mystyle');
    })

    // Make the current dropdown active
    target.className += 'mystyle'
}

// This adds click event listener to all dropdowns and tells it to fire callThisFunction when clicked

dropdowns.forEach(function (dropdown) {
    dropdown[i].addEventListener('click', callThisFunction, false);
})

Upvotes: 2

Pietro Nadalini
Pietro Nadalini

Reputation: 1800

If you want to do the functionality with JavaScript, you have to use ids in order to close the other dropdowns, once a new one is open.

So, the solution I've worked on is creating a new method closeMenus, which will close the other dropdowns once you execute toggleDropDown.

function myFunction() {
  var x = document.getElementById("myTopnav1");
  if (x.className === "topnav1") {
    x.className += " responsive1";
  } else {
    x.className = "topnav1";
  }
}

function toggleDropDown(myid) {
  closeMenus(myid);
  var element = document.getElementById(myid);
  element.classList.toggle("mystyle");  
}

function closeMenus(currentId) {
  var dropdowns = document.getElementsByClassName("dropdown-content1");
  var i;
  for (i = 0; i < dropdowns.length; i++) {
    var openDropdown = dropdowns[i];
    if (openDropdown.classList.contains('mystyle') && openDropdown.id !== currentId) {
      openDropdown.classList.remove('mystyle');
    }
  }
}

// Close the dropdown menu if the user clicks outside of it
window.onclick = function(event) {
  if (!event.target.matches('.dropbtn1')) {
    closeMenus();
  }
}
.topnav1 {
  background-color: blue !important;
}


/* Style the links inside the navigation bar */

.topnav1 a {
  float: left;
  display: block;
  color: black;
  text-align: center;
  padding: 4px 8px;
  text-decoration: none;
  font-size: 10px;
  border-bottom: 1px solid #FDFDFD;
}

.topnav-right1 {
  float: right;
}

@media only screen and (max-width:768px) {
  .topnav-right1 {
    float: left;
  }
}

.para-active {
  background-color: #4F7ADA !important;
  color: black !important;
}

.para-active button {
  color: white !important;
}


/* Add an active class to highlight the current page */

.active1 {
  color: black !important;
}


/* Hide the link that should open and close the topnav on small screens */

.topnav1 .icon {
  display: none;
}


/* Dropdown container - needed to position the dropdown content */

.dropdown1 {
  float: left;
  overflow: hidden;
  background-color: #f1f1f1;
  border-bottom: 1px solid #E3E3E3;
}


/* Style the dropdown button to fit inside the topnav */

.dropdown1 .dropbtn1 {
  font-size: 10px;
  border: none;
  outline: none;
  color: black;
  padding: 4px 8px;
  background-color: inherit;
  font-family: inherit;
  margin: 0;
  border-bottom: 1px solid #FDFDFD;
}


/* Style the dropdown content (hidden by default) */

.dropdown-content1 {
  display: none;
  position: absolute;
  background-color: #f9f9f9;
  min-width: 96px;
  box-shadow: 0px 8px 16px 0px rgba(0, 0, 0, 0.2);
  z-index: 1;
}

.mystyle {
  display: block;
}
<div class="topnav1" id="myTopnav1">
  <div class="dropdown1">
    <button style="display: none;" id="btn_em" onclick="toggleDropDown('div_em')" class="dropbtn1">Meters 
            </button>
    <div class="dropdown-content1" id="div_em">
      <label class="dropnav-container">one</label>
      <label class="dropnav-container">one</label>
      <label class="dropnav-container">one</label>
    </div>
  </div>
  <div class="dropdown1">
    <button id="btn_inv" onclick="toggleDropDown('div_inv')" class="dropbtn1">Inverters 
            </button>
    <div class="dropdown-content1" id="div_inv">
      <label class="dropnav-container">two</label>
      <label class="dropnav-container">two</label>
      <label class="dropnav-container">two</label>
    </div>
  </div>
  <div class="dropdown1">
    <button id="btn_inm" onclick="toggleDropDown('div_inm')" class="dropbtn1">Inverter Manager 
            </button>
    <div class="dropdown-content1" id="div_inm">
      <label class="dropnav-container">three</label>
      <label class="dropnav-container">three</label>
      <label class="dropnav-container">three</label>
    </div>
  </div>
  <div class="dropdown1">
    <button id="btn_ws" onclick="toggleDropDown('div_ws')" class="dropbtn1">Sensors 
            </button>
    <div class="dropdown-content1" id="div_ws">
      <label class="dropnav-container">four</label>
      <label class="dropnav-container">four</label>
      <label class="dropnav-container">four</label>
    </div>
  </div>
  <div class="dropdown1">
    <button id="btn_smu" onclick="toggleDropDown('div_smu')" class="dropbtn1">SMUs 
            </button>
    <div class="dropdown-content1" id="div_smu">
      <label class="dropnav-container">five</label>
      <label class="dropnav-container">five</label>
      <label class="dropnav-container">five</label> </div>
  </div>

  <a href="javascript:void(0);" class="icon" onclick="myFunction()">&#9776;</a>
</div>

Upvotes: 2

Ahmed Ali
Ahmed Ali

Reputation: 1966

I suggest you use JQUERY (CDN BELOW)

https://cdnjs.cloudflare.com/ajax/libs/jquery/3.4.1/jquery.js

Jquery is super fast and helpful
because your problem will be solved like this in jquery.

$('.yourdropdownsclass').on('click', function(){
   var target = $(this).attr('id');
   $("#"+target).show().siblings("div").hide();
});

.yourdropdownclass

is class of your dropdowns

$(this).attr('id')

this is getting the ids of another dropdowns.

$("#"+target).show().siblings("div").hide();

"target" is the name of ids of other dropdowns and storing it to variable and showing it and hiding another sibling's dropdowns. I hope it will help you!

Upvotes: 1

Related Questions