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

87
Views
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 answers
Answer question

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 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!