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

88
Vistas
Find specific elements by class in React

I am learning react and I have an array of strings that are classes of different elements,

let melody = ['a', 'c', 'f', 'g']

I am doing a piano and this is a list that conform a melody that should find each element that has the same class and play it getting the final melody. I would normally do it with something like (obviously with an interval and other stuff)

    melody.forEach(note=> {
document.getElementsByClassName(note)[0].click()
})

I am not sure if this is the best way to achieve this in react, and if using getByClass if correct to use in cases like this. Thank you so much....

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

0

I'll advise the use of modern DOM syntax.

let clickButton = document.querySelectorAll('.note');

let melody = ['a', 'c', 'f'];
    
melody.forEach((note) => {
    let newNote = document.querySelector("." + note);
    newNote.addEventListener("click", () => {
        alert("Hello world");
    });
});
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