Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

152
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda