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>
In the given code, we can toggle a div using a switch. I want to modify the code so that when the top div is vertical, all textboxes are displayed one below the other. When it's horizontal, the first 4 text boxes should be arranged in a row and then the next 4 text boxes, and so on. Essentially, I want to toggle and rearrange the text boxes simultaneously.
Rearranging and toggling fields using a toggle switch.