-1

How to get SVG of clicked element in page with multiple svgs.

I prefer not to go up the parentNode chain till I find it is an SVG node. Thanks.

bjd
  • 91
  • 1
  • 6

1 Answers1

1

The SVGElement interface exposes an ownerSVGElement property that will expose just that information:

document.querySelector('rect').onclick = function(evt) {
  console.log(this.ownerSVGElement.id);
};
click the rect<br>
<svg id="find_me">
  <rect width='200' height='200'/>
</svg>
Kaiido
  • 123,334
  • 13
  • 219
  • 285