2

I have a simple drop-down menu using JavaScript.

<div id="show-nav" class="dropdown">

    <div id="dropdown" onClick="myFunction()">Menu Name</div>

          <div id="myDropdown" class="dropdown-content">

                <a href="#option1">Option 1</a>
                <a href="#option2">Option 2</a>
                <a href="#option3">Option 3</a>

          </div>

       </div>

And the JS:

<script>

/* When the user clicks on the button, 
toggle between hiding and showing the dropdown content */
function myFunction() {
document.getElementById("myDropdown").classList.toggle("show");
}

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

var dropdowns = document.getElementsByClassName("dropdown-content");
var i;
for (i = 0; i < dropdowns.length; i++) {
  var openDropdown = dropdowns[i];
  if (openDropdown.classList.contains('show')) {
    openDropdown.classList.remove('show');
  }
}
}
}

</script>

I have multiple copies of the same menu in several adjacent divs. With this code, the menu works in the first element, but if I try to toggle it in the second, it only drops down in the first. How can I make this work for several menus on a single page?

1
  • 2
    html code would be helpful. Commented Feb 19, 2017 at 22:39

3 Answers 3

3

To the myFunction pass this. That is:

 <div id="dropdown" onClick="myFunction(this)">Menu Name</div>

Write a JavaScript function like below:

function myFunction(a) {
  a.parentNode.getElementsByClassName("dropdown-content")[0].classList.toggle("show");
}

No need of that much of JavaScript: keep as simple as possible.

Sign up to request clarification or add additional context in comments.

Comments

0

You need two changes on your code :

  • Make all buttons' onClick attributes same. (I guess it is "myFunction();")
  • Change your handler to process the button which is actually clicked.

This is possible with event.target :

function myFunction(event) {
  event.target.classList.toggle("show");
}

1 Comment

All the onClick attributes at the moment are like this: <div id="dropdown" onClick="myFunction()">Menu name</div> Are you saying I replace the first bit of code the above? Or that I need to add that?
0

You can simply do this without written any long javascript code.

use this

<div id="show-nav" class="dropbtn">

    <div id="dropdown" onClick="myFunction(this)">Menu Name</div>

          <div id="myDropdown" class="dropdown-content">

                <a href="#option1">Option 1</a>
                <a href="#option2">Option 2</a>
                <a href="#option3">Option 3</a>

          </div>

       </div>

       <div id="show-nav" class="dropbtn" style="margin-left:300px; margin-top:-16px;">

    <div id="dropdown" onClick="myFunction(this)">Menu Name</div>

          <div id="myDropdown" class="dropdown-content">

                <a href="#option1">Option 1</a>
                <a href="#option2">Option 2</a>
                <a href="#option3">Option 3</a>

          </div>

       </div>
function myFunction(a) {
  a.parentNode.getElementsByClassName('dropdown-content')[0].classList.toggle("show");
}
.dropdown {
position: absolute;
right:5%;
  overflow: hidden;
}

.dropdown .dropbtn {

  font-size: 16px;
  border: none;
  outline: none;


  background-color: inherit;
  font-family: inherit;
  margin: 0;
}


.dropdown-content {
  display: none;

  background-color: #f9f9f9;
  min-width: 50px;
  box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
  z-index: 1;
}

.dropdown-content a {
  float: none;
  color: black;
  padding: 12px 16px;
  text-decoration: none;
  display: block;
  text-align: left;
}




.show {
  display: block;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.