Home > Net >  Problem with flexbox within JS collapsible div
Problem with flexbox within JS collapsible div

Time:08-22

I am trying to create forms within collapsible divs using the following code. I would like the items in the collapsible fields to sit in neat rows using flexbox but i can't seem to get it to work (they currently sit in column despite flexbox). I apologise if this question is poorly asked-I'm a beginner.

var coll = document.getElementsByClassName("collapsible");
var i;

function toggleExpandCollapse() {
  document.querySelectorAll('.panel').forEach(function(el) {
    el.classList.toggle('hidden');
  });
}
for (i = 0; i < coll.length; i  ) {
  coll[i].addEventListener("click", function() {
    this.classList.toggle("active");
    var content = this.nextElementSibling;
    if (content.style.display === "block") {
      content.style.display = "none";
    } else {
      content.style.display = "block";
    }
  });
}
.collapsible {
  background-color: #eee;
  color: #444;
  cursor: pointer;
  padding: 18px;
  width: 100%;
  border: none;
  text-align: left;
  outline: none;
  font-size: 15px;
}

.collapsible:after {
  content: '\02795';
  /* Unicode character for "plus" sign ( ) */
  font-size: 13px;
  color: white;
  float: right;
  margin-left: 5px;
}

.active:after {
  content: "\2796";
  /* Unicode character for "minus" sign (-) */
}


/* Add a background color to the button if it is clicked on (add the .active class with JS), and when you move the mouse over it (hover) */

.active,
.collapsible:hover {
  background-color: #ccc;
}


/* Style the collapsible content. Note: hidden by default */

.content {
  padding: 0 18px;
  display: flex;
  flex-direction: row;
  display: none;
  align-items: left;
  justify-content: left;
}
<button type="button" >inventory</button>
<div >
  <div>input1</div>
  <div>input2</div>
  <div>input3</div>
  <div>input4</div>
</div>

CodePudding user response:

In the js code, instead of change the display to block make it change to flex.

  var coll = document.getElementsByClassName("collapsible");
    var i;
    function toggleExpandCollapse() { document.querySelectorAll('.panel').forEach(function(el) { el.classList.toggle('hidden'); }); }
    for (i = 0; i < coll.length; i  ) {
      coll[i].addEventListener("click", function() {
        this.classList.toggle("active");
        var content = this.nextElementSibling;
        if (content.style.display === "flex") {
          content.style.display = "none";
        } else {
          content.style.display = "flex";
        }
      });
    }
.collapsible {
    background-color: #eee;
    color: #444;
    cursor: pointer;
    padding: 18px;
    width: 100%;

    border: none;
    text-align: left;
    outline: none;
    font-size: 15px;
  }
  .collapsible:after {
    content: '\02795'; /* Unicode character for "plus" sign ( ) */
    font-size: 13px;

    color: white;
    float: right;
    margin-left: 5px;
  }
  .active:after {
    content: "\2796"; /* Unicode character for "minus" sign (-) */
  }
  /* Add a background color to the button if it is clicked on (add the .active class with JS), and when you move the mouse over it (hover) */
  .active, .collapsible:hover {
    background-color: #ccc;
  }
  
  /* Style the collapsible content. Note: hidden by default */

  .content {
    padding: 0 18px;
    display: flex;
    flex-direction: row;
    display: none;
    justify-content: space-between;
    align-items: center;
  }
<!DOCTYPE html>
<html>
   

<head>

<style>

</style>
</head>
<body>
<button type="button" >inventory</button>
<div >
  <div>input1</div>
  <div>input2</div>
  <div>input3</div>
  <div>input4</div>
  </div>

</body>

</html>

CodePudding user response:

Try adding this CSS (is this what you were asking for?):

.content {
  display: flex;
  flex-direction: row;
  width: 100%;
}

.content > div {
  flex-grow: 1;
}

And change these lines in your code:

content.style.display = "block";

To this:

content.style.display = "flex";
  • Related