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

116
Vistas
How to add click to items with same classes in pure javascript?

I don't know how I can catch individual elements with the same classes in pure javascript (without jQuery).

example:

<div class="item">
    <div class="divInside"></div>
</div>
<div class="item">
    <div class="divInside"></div>
</div>
<div class="item">
    <div class="divInside"></div>
</div>

Specifically, I want to catch a "divInside" for the "item" so that I can add a click function. Can someone help me?

Thank you in advance for your answer.

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

document.querySelectorAll('.divInside') will get you a list of all the elements in the document with the divInside class.

Adding a click event to all of them should be as easy as this:

const elems = document.querySelectorAll('.divInside')

elems.forEach(element => element.addEventListener('click', e => {
//Whatever you want to do
}))

What querySelectorAll does is it essentially lets you search for elements on the DOM using a matching CSS selector. If you want to get just the first element that matches a selector, you can use document.querySelector instead.

about 4 years ago · Juan Pablo Isaza Denunciar

0

You would use querySelectorAll followed by forEach to loop through and add the click events.

function clickEvent(e){
   console.log(e);
}

document.addEventListener('DOMContentLoaded', function(){
    document.querySelectorAll('.item > .divInside').forEach((div)=>{
        div.addEventListener('click', clickEvent);
    });
});
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