Search code examples
javascripthtmlcssrangeslider

Why does value of range slider not apply on div-container?


I'm trying to dynamically change the grid size of the div-container by using a range slider with JavaScript.

But as soon as the mouseup-event handler triggers, the value of range slider won't apply on the div-container. Why does value of range slider not apply on div-container?

What am I missing? Need some help please...

"use strict";

const divContainer = document.querySelector(".container");
const input = document.querySelector("input");
const outputs = document.querySelectorAll("output");
let slider = document.getElementById("gridSize");

const body = document.querySelector("body");
document.body.ondragstart = () => { return false };

function createGrid(col, rows) {
  for (let i = 0; i < (col * rows); i++) {
      const div = document.createElement("div");
      divContainer.style.gridTemplateColumns = `repeat(${col}, 1fr)`;
      divContainer.style.gridTemplateRows = `repeat(${rows}, 1fr)`;
      divContainer.appendChild(div).classList.add("box");
  }
}
createGrid(50,50);

input.addEventListener("input", () => {
  for (let output of outputs) {
    output.innerText = input.value;
  }
})

function modifyGridSize() {
  let boxes = divContainer.querySelectorAll(".box");
  boxes.forEach(box => box.remove());
  createGrid(slider.value);
}

slider.addEventListener("mouseup", modifyGridSize);
*,
*::before,
*::after {
  margin: 0;
  padding: 0;
  -webkit-box-sizing: border-box;
  box-sizing: border-box;
}

html {
  font-size: 16px;
}

body {
  background: linear-gradient(to bottom, #1488CC, #2B32B2);
  color: #FFF;
  line-height: 1.5;
  height: 100vh;
}

#wrapper {
  display: grid;
  grid-template-areas: "buttons container";
  grid-auto-columns: 50% 50%;
}

.container {
  width: 510px;
  height: 510px;
  display: grid;
  background-color: #FFF;
  box-shadow: 0 0 10px;
}

.box {
  border: .5px solid #808080;
}

.buttons {
  display: flex;
  flex-direction: column;
  justify-content: center;
  margin: 0 auto;
  gap: 10px;
}

.btn {
  width: 200px;
  height: 50px;
  font-family : inherit;
  font-size: 2rem;
  font-weight: bold;
  color: #000;
}

.btn:hover {
  box-shadow: 0 0 10px;
}

.btn:active {
  box-shadow: inset 0 0 8px;
}

.blackBtn {
  background: #000;
  color: #4C4C4C;
}

.greyBtn {
  background: linear-gradient(to right, #BDC3C7, #2C3E50);
}

.rainbowBtn {
  background: linear-gradient(to right, orange , yellow, green, cyan, blue, violet);
}

.eraseBtn {
  background: #FFF;
}

.clearCanvasBtn {
  background: #FF0000;
}

.shake {
  animation: shake .5s linear 1;
}

@keyframes shake {
  10%,
  90% {
    transform: translate3d(-1px, 0, 0);
  }
  20%,
  80% {
    transform: translate3d(2px, 0, 0);
  }
  30%,
  50%,
  70% {
    transform: translate3d(-4px, 0, 0);
  }
  40%,
  60% {
    transform: translate3d(4px, 0, 0);
  }
}

#wrapper label {
  font-size: 2rem;
}

.gridDimension {
  font-size: 1.5rem;
  text-align: center;
}

input[type="range"] {
  margin-bottom: 40px;
}
<!DOCTYPE html>
<html lang="en">

  <head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, shrink-to-fit=no">
    <title>Etch-A-Sketch</title>
    <link rel="stylesheet" type="text/css" href="src/css/etchAsketch.css">
  </head>

  <body>

      <main id="wrapper">
        <div class="buttons">
          <label for="gridSize">Select Grid Size:</label>
          <div class="gridDimension"><output>50</output> x <output>50</output></div>
          <input id="gridSize" type="range" name="gridSize" value="50" step="1" min="1" max="100">
        </div>
        <div class="container"></div>
      </main>

    <script src="etchAsketch.js"></script>

  </body>

</html>


Solution

  • createGrid accepts two parameters, col and rows. You only passed one parameter (col).

    This causes your for loop (for (let i = 0; i < (col * rows); i++)) to never execute since this statement: i < (col * rows) always returns false.

    This is because col * undefined returns undefined, thus causing number comparisons to return false.

    Use instead:

    createGrid(slider.value, slider.value);
    

    Demo:

    "use strict";
    
    const divContainer = document.querySelector(".container");
    const input = document.querySelector("input");
    const outputs = document.querySelectorAll("output");
    let slider = document.getElementById("gridSize");
    
    const body = document.querySelector("body");
    document.body.ondragstart = () => { return false };
    
    function createGrid(col, rows) {
      for (let i = 0; i < (col * rows); i++) {
          const div = document.createElement("div");
          divContainer.style.gridTemplateColumns = `repeat(${col}, 1fr)`;
          divContainer.style.gridTemplateRows = `repeat(${rows}, 1fr)`;
          divContainer.appendChild(div).classList.add("box");
      }
    }
    createGrid(50,50);
    
    input.addEventListener("input", () => {
      for (let output of outputs) {
        output.innerText = input.value;
      }
    })
    
    function modifyGridSize() {
      let boxes = divContainer.querySelectorAll(".box");
      boxes.forEach(box => box.remove());
      createGrid(slider.value, slider.value);
    }
    
    slider.addEventListener("mouseup", modifyGridSize);
    *,
    *::before,
    *::after {
      margin: 0;
      padding: 0;
      -webkit-box-sizing: border-box;
      box-sizing: border-box;
    }
    
    html {
      font-size: 16px;
    }
    
    body {
      background: linear-gradient(to bottom, #1488CC, #2B32B2);
      color: #FFF;
      line-height: 1.5;
      height: 100vh;
    }
    
    #wrapper {
      display: grid;
      grid-template-areas: "buttons container";
      grid-auto-columns: 50% 50%;
    }
    
    .container {
      width: 510px;
      height: 510px;
      display: grid;
      background-color: #FFF;
      box-shadow: 0 0 10px;
    }
    
    .box {
      border: .5px solid #808080;
    }
    
    .buttons {
      display: flex;
      flex-direction: column;
      justify-content: center;
      margin: 0 auto;
      gap: 10px;
    }
    
    .btn {
      width: 200px;
      height: 50px;
      font-family : inherit;
      font-size: 2rem;
      font-weight: bold;
      color: #000;
    }
    
    .btn:hover {
      box-shadow: 0 0 10px;
    }
    
    .btn:active {
      box-shadow: inset 0 0 8px;
    }
    
    .blackBtn {
      background: #000;
      color: #4C4C4C;
    }
    
    .greyBtn {
      background: linear-gradient(to right, #BDC3C7, #2C3E50);
    }
    
    .rainbowBtn {
      background: linear-gradient(to right, orange , yellow, green, cyan, blue, violet);
    }
    
    .eraseBtn {
      background: #FFF;
    }
    
    .clearCanvasBtn {
      background: #FF0000;
    }
    
    .shake {
      animation: shake .5s linear 1;
    }
    
    @keyframes shake {
      10%,
      90% {
        transform: translate3d(-1px, 0, 0);
      }
      20%,
      80% {
        transform: translate3d(2px, 0, 0);
      }
      30%,
      50%,
      70% {
        transform: translate3d(-4px, 0, 0);
      }
      40%,
      60% {
        transform: translate3d(4px, 0, 0);
      }
    }
    
    #wrapper label {
      font-size: 2rem;
    }
    
    .gridDimension {
      font-size: 1.5rem;
      text-align: center;
    }
    
    input[type="range"] {
      margin-bottom: 40px;
    }
    <!DOCTYPE html>
    <html lang="en">
    
      <head>
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1.0, shrink-to-fit=no">
        <title>Etch-A-Sketch</title>
        <link rel="stylesheet" type="text/css" href="src/css/etchAsketch.css">
      </head>
    
      <body>
    
          <main id="wrapper">
            <div class="buttons">
              <label for="gridSize">Select Grid Size:</label>
              <div class="gridDimension"><output>50</output> x <output>50</output></div>
              <input id="gridSize" type="range" name="gridSize" value="50" step="1" min="1" max="100">
            </div>
            <div class="container"></div>
          </main>
    
        <script src="etchAsketch.js"></script>
    
      </body>
    
    </html>