Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add <a> link to svg path to navigate to another svg image using javascript

I am new to svg. I have two svg images i need to append link around the path in one svg image, onclicking that path i should navigate to another svg image.

<svg id="svg1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" >
    <g id="path1">
     <a xlink:href="" target="_top">
       <path id="parcel" />
     </a>
     </g>
 </svg>

How should i get attribute into my application(html) and append href link to it using javascript. Thanks in advance

like image 761
user2083041 Avatar asked Nov 02 '25 12:11

user2083041


1 Answers

Get a reference to the <a> element using whichever method you choose. For example

 <a id="mylink" xlink:href="" target="_top">
   <path id="parcel" />
 </a>

var  mylink = document.getElementById("mylink");

Then you can set the xlink:href attribute using:

mylink.setAttributeNS('http://www.w3.org/1999/xlink', 'xlink:href',
                      'http://www.google.com');
like image 90
Paul LeBeau Avatar answered Nov 04 '25 03:11

Paul LeBeau



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!