Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

92
Views
Highlight elements in a css grid layout using javascript

I got a css grid with 25 div elements. Each element should switch between being highlighted and normal when clicked on.

Since each element can be individually turned on and off, I made a box_state variable for each and set it to false as initial value so it can be set to true once its turned on.

To highlight each element I got this block of code:

    document.querySelector("#n_1").onclick = function() {
    if (box_1_state === false) {
        document.querySelector("#n_1").style.filter = "brightness(150%)";
        box_1_state = true;
    } else {
        document.querySelector("#n_1").style.filter = "brightness(100%)";
        box_1_state = false;
    }

} 

n_1 is the name of the div in the html file and box_1_state is the variable mentioned above.

So to make this work I have to make the code block above 25 times which kind of seems it could be done easier. Any suggestions?

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

  • All you need is Element.classList.toggle() and a specific CSS class, i.e: "is-active"
  • PS: filter brightness expects a maximal value of 100%

const ELS_box = document.querySelectorAll(".box");
ELS_box.forEach(EL => EL.addEventListener("click", function() {
  this.classList.toggle("is-active");
}))
.boxes {
  display: grid;
  grid-template-columns: repeat(5, 30px);
  grid-template-rows: repeat(5, 30px);
  grid-auto-flow: column;
}

.box {
  margin: 5px;
  background: red;
}

.box.is-active {
  filter: brightness(50%);
}
<div class="boxes">
  <div class="box"></div>
  <div class="box"></div>
  <div class="box"></div>
  <div class="box"></div>
  <div class="box"></div>
  <div class="box"></div>
  <div class="box"></div>
  <div class="box"></div>
  <div class="box"></div>
  <div class="box"></div>
  <div class="box"></div>
  <div class="box"></div>
  <div class="box"></div>
  <div class="box"></div>
  <div class="box"></div>
  <div class="box"></div>
  <div class="box"></div>
  <div class="box"></div>
  <div class="box"></div>
  <div class="box"></div>
  <div class="box"></div>
  <div class="box"></div>
  <div class="box"></div>
  <div class="box"></div>
  <div class="box"></div>
</div>

about 4 years ago · Juan Pablo Isaza Report

0

If you don't want to use a class, you can store its state in a custom attribute:

const divs = document.querySelectorAll('div');
divs.forEach(e => {
  e.addEventListener('click', function() {
    if (e.dataset.brightened != "true") {
      e.style.filter = "brightness(150%)";
      e.dataset.brightened = "true";
    } else {
      e.style.filter = "brightness(100%)";
      e.dataset.brightened = "false";
    }
  })
})
div {
  height: 100px;
  width: 100px;
  border: 1px solid;
  background-color: #ededed;
}
<div></div>
<div></div>
<div></div>

about 4 years ago · Juan Pablo Isaza Report

0

A method to automate this is by surrounding your code with a loop, and then use the loop's iterator/index to use as the querySelector

let numberOfDivElements = 25;
let box_states = [];

for (let iterator = 0; iterator < numberOfDivElements; iterator++) {
    // Initialize an array to store your "box states", whether true or false
    box_states[iterator] = false;
}

for (let iterator = 0; iterator < numberOfDivElements; iterator++) {
    let boxSelector = "#n_" + iterator;

    // Your example code
    document.querySelector(boxSelector).onclick = function() {
        if (box_states[iterator] === false) {
            document.querySelector(boxSelector).style.filter = "brightness(150%)";
            box_states[iterator] = true;
        } else {
            document.querySelector(boxSelector).style.filter = "brightness(100%)";
            box_states[iterator] = false;
        }
    }
    // End of your example code

}
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!