Philip Scriver
Philip Scriver

Reputation: 41

Open multiple dropdowns with the same Js function

I have these dropdowns at the left side of my page that are meant to display product details

User should be able to open them simultaneously meaning that when I open the 1st one the others are pushed down in order to be visible and be able to open too

Fiddle here https://jsfiddle.net/cazg8xwj/3/

<div class="container">
        <div class="a">
        <center>
          <div class="dropdown">
          <button onclick="myFunction()" class="dropbtn">Dropdown</button>
          <div id="myDropdown" class="dropdown-content">
              <img src="images/E11A.png" class="bottles"/>
              <img src="images/E11A.png" class="bottles"/>
          </div>
           </div>

          <button onclick="myFunction()" class="dropbtn">Dropdown</button>
          <div id="myDropdown" class="dropdown-content">
              <img src="images/E11A.png" class="bottles"/>
              <img src="images/E11A.png" class="bottles"/>
          </div>
         </div>
       </center>

     </div>

Stylesheet

* {
  box-sizing: border-box;
}

body {
  background-color: grey;
  background-size: 100%;
  background-image:url('g50.png');
  background-repeat: repeat;
}

html,
body {
  height: 100%;
  margin: 0px;
}

.container {
  width: 100%;
  height: 1080px;
  display: flex;
  flex-direction: row;
}

.a {
  float: left;
  width: 250px;
  height: 100%;
  border: 1px solid blue;
}

.A-content {
    margin:7px;
}


.dropbtn {
  width: 98%;
  padding: 14px;
  background-color: #3498DB;
  color: white;
  padding: 14px;
  font-size: 16px;
  border: none;
  cursor: pointer;
}

.dropbtn:hover, .dropbtn:focus {
  background-color: #2980B9;
}

.dropdown {
  width: 100%;
  display: inline-block;
}

.dropdown-content {
    width: 100%;
    height: 200px;
    overflow: scroll;
    display: none;
    background-color: #f1f1f1;
    box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
    z-index: 1;
}

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

.dropdown a:hover { background-color: #ddd; }

.show { display: block; }

Js

function myFunction() {
  document.getElementById("myDropdown").classList.toggle("show");
}

// Close the dropdown if the user clicks outside of it
window.onclick = function(event) {
  if (!event.target.matches('.dropbtn')) {
    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');
      }
    }
  }
}

Upvotes: 0

Views: 325

Answers (2)

avia
avia

Reputation: 1568

$('.dropbtn').click(function() {
    $('.dropdown-content').slideToggle();
});
* {
  box-sizing: border-box;
}

body {
  background-color: grey;
  background-size: 100%;
  background-image:url('g50.png');
  background-repeat: repeat;
}

html,
body {
  height: 100%;
  margin: 0px;
}

.container {
  width: 100%;
  height: 1080px;
  display: flex;
  flex-direction: row;
}

.a {
  float: left;
  width: 250px;
  height: 100%;
  border: 1px solid blue;
}

.A-content {
    margin:7px;
}


.dropbtn {
  width: 98%;
  padding: 14px;
  background-color: #3498DB;
  color: white;
  padding: 14px;
  font-size: 16px;
  border: none;
  cursor: pointer;
}

.dropbtn:hover, .dropbtn:focus {
  background-color: #2980B9;
}

.dropdown {
  width: 100%;
  display: inline-block;
}

.dropdown-content {
    width: 100%;
    height: 200px;
    overflow: scroll;
    display: none;
    background-color: #f1f1f1;
    box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
    z-index: 1;
}

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

.dropdown a:hover { background-color: #ddd; }

.show { display: block; }
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="container">
        <div class="a">
        <center>
          <div class="dropdown">
          <button id="dropbtn1" class="dropbtn">Dropdown</button>
          <div id="myDropdown1" class="dropdown-content">
              <img src="images/E11A.png" class="bottles"/>
              <img src="images/E11A.png" class="bottles"/>
          </div>
           </div>

          <button id="dropbtn2" class="dropbtn">Dropdown</button>
          <div id="myDropdown2" class="dropdown-content">
              <img src="images/E11A.png" class="bottles"/>
              <img src="images/E11A.png" class="bottles"/>
          </div>
         </div>
       </center>

     </div>

Upvotes: 1

Adrian Biba
Adrian Biba

Reputation: 267

when #myDropdown get class show it need to be display:inline-block instead of display:block, because you did the panel with position: absolute you have to change width to see all the content inside.

/* 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 if the user clicks outside of it
window.onclick = function(event) {
  if (!event.target.matches('.dropbtn')) {
    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');
      }
    }
  }
}
* {
  box-sizing: border-box;
}

body {
  background-color: grey;
  background-size: 100%;
  background-image:url('g50.png');
  background-repeat: repeat;
}

html,
body {
  height: 100%;
  margin: 0px;
}

.container {
  width: 100%;
  height: 1080px;
  display: flex;
  flex-direction: row;
}

.a {
  float: left;
  width: 250px;
  height: 100%;
  border: 1px solid blue;
}

.A-content {
    margin:7px;
}

.flexC {
  display: flex;
  flex-direction: column;
  flex-grow: 1;
}

.b {
  float: left;
  height: 60%;
  border: 1px solid blue;
  flex-grow: 1;
}

.c {
  float: left;
  height: 40%;
  border: 1px solid blue;
  flex-grow: 1;
}

.d {
  float: right;
  width: 50px;
  height: 100%;
  border: 1px solid blue;
}

.dropbtn {
  width: 98%;
  padding: 14px;
  background-color: #3498DB;
  color: white;
  padding: 14px;
  font-size: 16px;
  border: none;
  cursor: pointer;
}

.dropbtn:hover, .dropbtn:focus {
  background-color: #2980B9;
}

.dropdown {
  width: 100%;
  display: inline-block;
}

.dropdown-content {
    width: 100%;
    height: 200px;
    overflow: scroll;
    display: none;
    position: absolute;
    background-color: #f1f1f1;
    box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
    z-index: 1;
}

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

.dropdown a:hover { background-color: #ddd; }

.show { display: inline-block;}
<div class="container">
        <div class="a">
        <center>
          <div class="dropdown">
          <button onclick="myFunction()" class="dropbtn">Dropdown</button>
          <div id="myDropdown" class="dropdown-content">
              <img src="images/E11A.png" class="bottles"/>
              <img src="images/E11A.png" class="bottles"/>
          </div>
           </div>

          <button onclick="myFunction()" class="dropbtn">Dropdown</button>
          <div id="myDropdown" class="dropdown-content">
              <img src="images/E11A.png" class="bottles"/>
              <img src="images/E11A.png" class="bottles"/>
          </div>

          <button onclick="myFunction()" class="dropbtn">Dropdown</button>
          <div id="myDropdown" class="dropdown-content">
              <img src="images/E11A.png" class="bottles"/>
              <img src="images/E11A.png" class="bottles"/>
          </div>
         </div>
       </center>

     </div>
        <!-- Middle Flex Container -->
        <div class="flexC">
            <div class="b">
              text b
            </div>
            <div class="c">
              text c
            </div>
        </div>

        <div class="d">
          Invoice
        </div>
    </div>

Upvotes: 1

Related Questions