Change the Page layout when a button is clicked

Viewed 81

I want to change the layout of a page that has 3 columns:

<div>
    <div class="container">
      <div class="row" >
        <div class="col-md-4"></div>
        <div class="col-md-4"></div>
        <div class="col-md-4"></div>
      </div>
    </div>
  </div>

... to 4 columns when a button is clicked:

<div>
    <div class="container">
      <div class="row" >
        <div class="col-md-3"></div>
        <div class="col-md-3"></div>
        <div class="col-md-3"></div>
        <div class="col-md-3"></div>
      </div>
    </div>
  </div>

I have no clue on how to do this.

4 Answers

There are many ways you can add another div. Here is my approach :

function appendDiv(){
  let row = document.getElementsByClassName('row');
//   change className for all the col-md-4 div
  document.querySelectorAll('.col-md-4').forEach(function(item) {
   item.className = 'col-md-3';
  })
  //create new div;
  let col = document.createElement('div');
//   add classname to div
  col.className = "col-md-3"
  row[0].appendChild(col)
}
    .col-md-4{
  border : 1px solid blue;
      height : 20px;
}
    .col-md-3{
       border : 1px solid green;
      height : 20px;
    }
 <div>
    <div class="container">
      <div class="row" >
        <div class="col-md-4"></div>
        <div class="col-md-4"></div>
        <div class="col-md-4"></div>
      </div>
      <button onClick='appendDiv()'>click</button>
    </div>
  </div>

There's a few ways this could be done depending on your data, however, here's one angle.

If you have both your 4 column & 3 column versions of the data loaded on the page (but one hidden with css). You could run something like this.

HTML

<div id="colsThree" class="displayArea show">
  <div class="container">
    <div class="row" >
      <div class="col-md-4"></div>
      <div class="col-md-4"></div>
      <div class="col-md-4"></div>
    </div>
  </div>
</div>

<div id="colsFour" class="displayArea">
  <div class="container">
    <div class="row" >
      <div class="col-md-4"></div>
      <div class="col-md-4"></div>
      <div class="col-md-4"></div>
    </div>
  </div>
</div>

<button id="changeColumns">Click Me To Change Columns</button>

Javascript

const buttonEl = document.querySelector("#changeColumns");

buttonEl.addEventListener('click', () => {
  const outputEls = document.querySelectorAll('.displayArea')
  outputEls.forEach((outputEl) => {
    outputEl.toggle("show")
  })
});

CSS

.displayArea {
  display: none;
}

.displayArea.show {
  display: block;
}

Use forEach and appendChild method.

const btn = document.querySelector('#btn')

btn.onclick = function() {
const targetClasses = document.querySelectorAll('.col-md-4')
targetClasses.forEach((tag, idx) => {
tag.className = 'col-md-3'

const lastIdx = targetClasses.length - 1
if (idx === lastIdx) {
const tag = document.createElement('div')
, row = document.querySelector('.row')
tag.className = 'col-md-3'
tag.innerText = '4'
row.appendChild(tag)
}
})

console.log(targetClasses)

return
}
<div>
<button id="btn">Click me</button>
    <div class="container">
      <div class="row" >
        <div class="col-md-4">1</div>
        <div class="col-md-4">2</div>
        <div class="col-md-4">3</div>
      </div>
    </div>
  </div>

If you're only using vanilla HTML, CSS, and JavaScript, then one of the ways to achieve this is by adding a click listener to the button beforehand. FYI: for brevity's sake, I'll call the div element with row class as parent. When user clicks the button, then it should

  1. remove col-md-4 class and add col-md-3 class to all the children elements of parent.
  2. add a new div element with col-md-3 class into parent.

Here's a link to the codepen for your reference.

const button = document.querySelector('button');
const rowDiv = document.querySelector('.row');

button.addEventListener('click', (e) => {
  Array.from(rowDiv.children).forEach(childDiv => {
    childDiv.classList.remove('col-md-4');
    childDiv.classList.add('col-md-3');
  });
  const newDiv = document.createElement('div');
  newDiv.classList.add('col-md-3');
  rowDiv.appendChild(newDiv);
  
  // I disabled the button to prevent the user
  // from clicking it the second time.
  e.target.disabled = true;
});
.button-parent {
  margin: 15px 0;
}

.row {
  height: 100vh;
}

.row > div:nth-child(1) {
  background: red;
}

.row > div:nth-child(2) {
  background: blue;
}

.row > div:nth-child(3) {
  background: yellow;
}

.row > div:nth-child(4) {
  background: green;
}
<head>
  <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous">
</head>
<body>
  <div>
    <div class="container">
      <div class="button-parent">
        <button class="btn btn-primary">Add div</button>
      </div>
      <div class="row">
        <div class="col-md-4"></div>
        <div class="col-md-4"></div>
        <div class="col-md-4"></div>
      </div>
    </div>
  </div>
</body>

Related