How to resolve this issue - div onclick not returning id or data-id for either target or currentTarget?How to resolve this issue - div onclick not returning id or data-id for either target or currentTarget?

329    Asked by AashnaSaito in Salesforce , Asked on Apr 27, 2023

No matter what I try the event param will not give back the id set in the template. Here is the pertinent code...

Template

<!-- have also tried -->

Javascript

handleListClick(event) { console.log('bw: id = ' + event.currentTarget.id); // output: bw: id = undefined // have also tried console.log('bw: id = ' + event.currentTarget.dataset.id); // output: bw: id = undefined //have also tried let t = event.currentTarget; let idstr = t.getAttribute("id"); console.log('bw: id = ' + idstr); // output: bw: id = null

any time I used data-id I used a dataset when trying to retrieve the value.

Also in the inspector on chrome if I look at the element, neither the id nor the data-id shows the element tree. 

Answered by Abdul Rehman

To resolve the issue - div onclick not returning id or data-id for either target or currentTarget, you must understand that -

The problem with your code is in the key attribute you have passed {it.value.id} whereas for data-id you have passed {it.value.Id}. Both are different as you have used capital and small i . JavaScript is case sensitive. Check the correct api name for id. Use exactly in your html code.

event.currentTarget.dataset.id or event.target.dataset.id would log the value of data-id.


Your Answer

Interviews

Parent Categories