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

158
Views
Target multiple div ID in the same function/event

I'm trying to run a function to allows several divs, each with it's own unique ID, in the same call. I thought this would work:

var btn1 = document.getElementById("btn1");
btn1.onmouseover = function(event){
    event.preventDefault();
    toggleDivs("ed1", "ed2", "ed3");
};
//function to hide or show
function toggleDivs(s){
    //reset
    document.getElementById("ed1").classList.remove("shown");
    //show
    document.getElementById(s).classList.add("shown");
}

It kind of works, but only the first div in the array (ed1) appears. The other two and I'm sure any more if I added them to the overall array, would not.

I tried having a toggleDivs for each div to appear (3 in all in this case), but no luck. What would be the way to get these to all appear at once instead of just the first one?

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

0

In toggleDivs you are only using the first parameter. Use an array to send all:

toggleDivs([ "ed1", "ed2", "ed3" ]);

The document.getElementById(id) function only receives one id at a time, so you have to iterate like this:

s.forEach(id => document.getElementById(id).classList.add("displayed"))
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!