Home > Net >  SVG: Get clicked element
SVG: Get clicked element

Time:12-01

How to get the specific element inside SVG on which click was made?

    this.svgEl = document.createElementNS('http://www.w3.org/2000/svg', 'svg');        
    var s = Snap(this.svgEl);        
    var svgFromApi = (new DOMParser()).parseFromString("svg image from API", "image/svg xml");
    s.clear();
    s.append(svgFromApi.documentElement);
    this.divElementRef.nativeElement.appendChild(this.svgEl); 

    this.svgElement.onclick = ()=> {               
           //Need to get on which element inside SVG it was clicked.   
       }

CodePudding user response:

Try this

this.svgEl.onclick = (event) => {               
   // event.target should contain the element that was clicked on
}
  • Related