0

In onClick event i capture the event(e) in a function and trying to get the name of the element that was clicked

const ClickEvent = (e) => {
console.log(e.target.name) -> undefined
}

<svg>
<text onClick={ClickEvent} name="line1">
 hello world
</text>
</svg>
Alex Wayne
  • 162,909
  • 46
  • 287
  • 312
Yehuda Zvi
  • 83
  • 2
  • 6

3 Answers3

2

DOM attributes are not always available as direct named properties of the element. I would advise you to use the getAttribute() method to fetch the values of the attributes you want.

console.log(e.target.getAttribute('name'))
Alex Wayne
  • 162,909
  • 46
  • 287
  • 312
0

The best option would be to use the getAttributes method, as the DOM can be a little finicky sometimes.

const ClickEvent = (e) => {
   console.log(e.target.getAttribute('name'))
}

Similar question for more reference: event.target.name is undefined

Lowgy
  • 93
  • 1
  • 7
0

There is no e.target.name method, try e.target and check the console to find the method you want.

I guess you mean e.target.localName?

Chris
  • 112,704
  • 77
  • 249
  • 231
MaxPL
  • 1