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

172
Views
Why doesn't my code work with querySelectorAll?

When I try to create a simple alert by using document.querySelector('.custom'), my code functions properly and returns the dataset of first img but doesn't for the other line(s) although I change the JS into document.querySelectorAll('.custom')

I'm sure it's kids' stuff for most but I couldn't find the solution though. Thanks in advance for your help.

const custom = document.querySelectorAll('.custom');
custom.addEventListener('click', function() {
    alert(`Welcome ${custom.dataset.name} ${custom.dataset.last}`);
});
<div class="item">
      <img data-name="name 1" data-last="last 1" class="custom" src="https://source.unsplash.com/random/300x300" />
      <img data-name="name 2" data-last="last 2" class="custom" src="https://source.unsplash.com/random/300x300" />
      <p>Hi I'm a item</p>
    </div>

about 4 years ago · Juan Pablo Isaza
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!