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

127
Views
Event.target.id returns empty string

Hello I have a problem with returning an id value (defined in the key attribute) of the image when using event.target.

function imageClicked(event) {
    console.log(event.target.key);
    setClicked(event.target.key);
    shuffle(GameData);
    if (clicked.includes(event.target.key) === false) {
      setScore(score + 1);
      setClicked([...clicked, event.target.key]);
    } else {
      setScore(0);
      setClicked([]);
    }
  }

...

 <Display onClick={imageClicked} items={GameData} />

And component fragment:

<div className="display">
      <img
        src={props.image}
        alt={props.name}
        key={props.id}
        onClick={props.onClick}
      />
      <p>{props.name}</p>
    </div>

However when I am using an alt attribute of the image the app works as intended. But I want to change it anyway as I creacted ids for all images and they are currently unused.

Thanks in advance.

Here is the link to the full app (using alt attribute) in CodeSandbox.

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!