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

305
Views
Cómo saber en qué elemento de querySelectorAll se hizo clic

Tengo esta estructura de código:

 let items = document.querySelectorAll('.item') items.forEach(addEventListener ( 'click', function () { // code that will get item that got clicked } ))

El HTML del elemento se ve así:

 <div class="item" data-value="water">water</div> <div class="item" data-value="air">air</div> <div class="item" data-value="fire">fire</div>

Y quiero tener una función que cuando haga clic en cualquier elemento, el atributo de datavalue de datos del elemento debe mostrarse en la consola o en cualquier lugar. Solo debo saber en qué elemento se hizo clic.

probé esto

 items.forEach(addEventListener ( 'click', function () { console.log(this.getAttribute('data-value')) } ))

Pero no funcionó..

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Intenta lo siguiente:

 let items = document.querySelectorAll('.item') items.forEach(item => item.addEventListener ( 'click', function () { console.log(event.target.getAttribute('data-value')) } ))
 <div class="item" data-value="water">water</div> <div class="item" data-value="air">air</div> <div class="item" data-value="fire">fire</div>

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!