Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

96
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda