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 add a link around the path in one svg image, by clicking that path, I have to go 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 do I get an attribute in my application (html) and add a href link to it using javascript. thanks in advance

+3


source to share


1 answer


Get a link to an item <a>

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 attribute xlink:href

using:

mylink.setAttributeNS('http://www.w3.org/1999/xlink', 'xlink:href',
                      'http://www.google.com');

      

+3


source







All Articles