skip to Main Content
function toggleDivs() {
  var container = document.querySelector(".container");
  var top = document.querySelector(".top");
  var bottom = document.querySelector(".bottom");

  if (container.style.flexDirection === "column") {
    container.style.flexDirection = "row";
    top.style.flex = "3";
    bottom.style.flex = "7";
    top.style.height = "auto";
    bottom.style.height = "auto";
  } else {
    container.style.flexDirection = "column";
    top.style.flex = "3";
    bottom.style.flex = "7";
    top.style.height = "100%";
    bottom.style.height = "100%";
  }
}
.container {
  display: flex;
  flex-direction: column;
  height: 100vh;
}

.top {
  flex: 3;
  background-color: blue;
  border: 1px solid black;
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: center;
}

.bottom {
  flex: 7;
  background-color: green;
  border: 1px solid black;
}

input[type="checkbox"] {
  height: 0;
  width: 0;
  visibility: hidden;
}

label {
  cursor: pointer;
  text-indent: -9999px;
  width: 50px;
  height: 25px;
  background-color: grey;
  display: block;
  border-radius: 100px;
  position: relative;
}

label:before {
  content: "";
  position: absolute;
  top: 1px;
  left: 1px;
  width: 23px;
  height: 23px;
  background-color: #fff;
  border-radius: 90px;
  transition: 0.3s;
}

input:checked + label:before {
  left: calc(100% - 1px);
  transform: translateX(-100%);
}

.toggle-button {
  margin-top: 10px;
  padding: 10px;
  background-color: #4caf50;
  color: white;
  border: none;
  border-radius: 5px;
  cursor: pointer;
}

.toggle-button:hover {
  background-color: #3e8e41;
}

input[type="text"] {
  margin-bottom: 10px; /* added this */
}

@media screen and (min-width: 768px) {
  .container {
    flex-direction: row;
  }

  input[type="text"] {
    margin-bottom: 0; /* added this */
  }
}
<input type="checkbox" id="toggle-switch" onclick="toggleDivs()" />
<label for="toggle-switch"></label>

<div class="container">
  <div class="top">
    <input type="text" placeholder="Text box 1" />
    <input type="text" placeholder="Text box 2" />
    <input type="text" placeholder="Text box 3" />
    <input type="text" placeholder="Text box 4" />
    <input type="text" placeholder="Text box 5" />
    <input type="text" placeholder="Text box 6" />
    <input type="text" placeholder="Text box 7" />
    <input type="text" placeholder="Text box 8" />
  </div>
  <div class="bottom"></div>
</div>

as we can see in above code we are able to toggle div using toggle switch. Im looking to fix this code in such a way that when top div is vertical I would like to have all the textboxes one below each other and when its horizontal, then we can rearrange first 4 text boxes in single row and then next 4 text boxes and so on..
in short how we can toggle and rearrange text boxes at the same time.

toggling and rearranging fields using toggle switch.

2

Answers


  1. When the top div is in a horizontal layout, you can modify the toggleDivs() function:-

        function toggleDivs() {
          var container = document.querySelector(".container");
          var top = document.querySelector(".top");
          var bottom = document.querySelector(".bottom");
        
          if (container.style.flexDirection === "column") {
            container.style.flexDirection = "row";
            top.style.flex = "3";
            bottom.style.flex = "7";
            top.style.height = "auto";
            bottom.style.height = "auto";
            // Remove the textbox-row class to allow rearranging
            var textboxRows = document.querySelectorAll(".textbox-row");
            textboxRows.forEach((row) => {
              row.style.display = "flex";
            });
          } else {
            container.style.flexDirection = "column";
            top.style.flex = "3";
            bottom.style.flex = "7";
            top.style.height = "100%";
            bottom.style.height = "100%";
            // Add the textbox-row class to stack the textboxes vertically
            var textboxRows = document.querySelectorAll(".textbox-row");
            textboxRows.forEach((row) => {
              row.style.display = "block";
            });
          }
        }
        .container {
          display: flex;
          flex-direction: column;
          height: 100vh;
        }
        
        .top {
          flex: 3;
          background-color: blue;
          border: 1px solid black;
          display: flex;
          flex-wrap: wrap;
          align-items: center;
          justify-content: center;
        }
        
        .bottom {
          flex: 7;
          background-color: green;
          border: 1px solid black;
        }
        
        .textbox-row {
          display: flex; /* Added this */
          flex-wrap: wrap; /* Added this */
        }
        
        .textbox-row input[type="text"] {
          margin: 5px; /* Adjusted this */
        }
        
        input[type="checkbox"] {
          height: 0;
          width: 0;
          visibility: hidden;
        }
        
        label {
          cursor: pointer;
          text-indent: -9999px;
          width: 50px;
          height: 25px;
          background-color: grey;
          display: block;
          border-radius: 100px;
          position: relative;
        }
        
        label:before {
          content: "";
          position: absolute;
          top: 1px;
          left: 1px;
          width: 23px;
          height: 23px;
          background-color: #fff;
          border-radius: 90px;
          transition: 0.3s;
        }
        
        input:checked + label:before {
          left: calc(100% - 1px);
          transform: translateX(-100%);
        }
        
        .toggle-button {
          margin-top: 10px;
          padding: 10px;
          background-color: #4caf50;
          color: white;
          border: none;
          border-radius: 5px;
          cursor: pointer;
        }
        
        .toggle-button:hover {
          background-color: #3e8e41;
        }
        
        @media screen and (min-width: 768px) {
          .container {
            flex-direction: row;
          }
        
          .textbox-row {
            display: block; /* Adjusted this */
          }
        
          .textbox-row input[type="text"] {
            margin: 0 5px 0 0; /* Adjusted this */
          }
        
          input[type="text"] {
            margin-bottom: 0;
          }
        }
       <input type="checkbox" id="toggle-switch" onclick="toggleDivs()" />
        <label for="toggle-switch"></label>
        
        <div class="container">
          <div class="top">
            <div class="textbox-row">
              <input type="text" placeholder="Text box 1" />
              <input type="text" placeholder="Text box 2" />
              <input type="text" placeholder="Text box 3" />
              <input type="text" placeholder="Text box 4" />
            </div>
            <div class="textbox-row">
              <input type="text" placeholder="Text box 5" />
              <input type="text" placeholder="Text box 6" />
              <input type="text" placeholder="Text box 7" />
              <input type="text" placeholder="Text box 8" />
            </div>
          </div>
          <div class="bottom"></div>
        </div>

    There is additional two div elements with the class "textbox-row" to group the textboxes. When the top div is in a horizontal layout, these rows will be displayed as flex containers, allowing the textboxes to be rearranged in a row-based layout. When the top div is in a vertical layout, the "textbox-row" class is removed, and the textboxes will stack vertically as they are block elements by default.

    Working Link Tested:- Codesandbox Code

    Login or Signup to reply.
  2. I suspect that what you are trying to achieve should be done with a grid layout.

    Due to the comments, I spent an hour figuring out how to modify the JavaScript to add a class instead of the inline style. The use of a media query to declare the initial grid-template-columns value makes the JS and CSS a bit more complex with if..else if..else statement, .row class and !important flag.

    function toggleColumns() {
      const top = document.getElementById("top");
      console.log(top.outerHTML);
    
      if (top.classList.contains("rows")) {
        top.classList.toggle("rows");
        top.classList.toggle("columns");
       }
       
      else if (top.classList.contains("columns")) {
        top.classList.toggle("rows");
        top.classList.toggle("columns");
       }
       
      else {
        top.classList.toggle("columns");
      }
    
    }
    .container {
      height: 100vh;
      background-color: green;
    }
    
    .top {
      display: grid;
      align-items: center;
      justify-content: center;
      background-color: blue;
      border: 1px solid black;
      gap: 1em;
      padding: 1em;
    }
    
    .columns {
      grid-template-columns: repeat(4, 1fr) !important;
    }
    
    .rows {
      grid-template-columns: auto !important;
    }
    
    input[type="checkbox"] {
      height: 0;
      width: 0;
      visibility: hidden;
    }
    
    label {
      cursor: pointer;
      text-indent: -9999px;
      width: 50px;
      height: 25px;
      background-color: grey;
      display: block;
      border-radius: 100px;
      position: relative;
    }
    
    label:before {
      content: "";
      position: absolute;
      top: 1px;
      left: 1px;
      width: 23px;
      height: 23px;
      background-color: #fff;
      border-radius: 90px;
      transition: 0.3s;
    }
    
    input:checked + label:before {
      left: calc(100% - 1px);
      transform: translateX(-100%);
    }
    
    .toggle-button {
      margin-top: 10px;
      padding: 10px;
      background-color: #4caf50;
      color: white;
      border: none;
      border-radius: 5px;
      cursor: pointer;
    }
    
    .toggle-button:hover {
      background-color: #3e8e41;
    }
    
    input[type="text"] {
      margin-bottom: 10px;
    }
    
    @media screen and (min-width: 768px) {
      .top {
        grid-template-columns: repeat(4, 1fr);
      }
    
      input[type="text"] {
        margin-bottom: 0;
      }
    }
    <input type="checkbox" id="toggle-switch" onclick="toggleColumns()" />
    <label for="toggle-switch"></label>
    
    <div class="container">
      <div id="top" class="top">
        <input type="text" placeholder="Text box 1" />
        <input type="text" placeholder="Text box 2" />
        <input type="text" placeholder="Text box 3" />
        <input type="text" placeholder="Text box 4" />
        <input type="text" placeholder="Text box 5" />
        <input type="text" placeholder="Text box 6" />
        <input type="text" placeholder="Text box 7" />
        <input type="text" placeholder="Text box 8" />
      </div>
    </div>
    
    
    function toggleColumns() {
      const top = document.getElementById("top");
      console.log(top.outerHTML);
    
      if (top.classList.contains("rows") {
        top.classList.toggle("rows");
       } else {
        top.classList.toggle("columns");
      }
    }
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search