Rickard
Rickard

Reputation: 436

snap svg add/remove link attribute

I am able to manipulate the xlink:href attribute of an SVG link element with ECMAScript like this :

var xlinkns = "http://www.w3.org/1999/xlink";

myLink.setAttributeNS(xlinkns, "xlink:href", "#");//add href attribute to the link

myLink.removeAttributeNS(xlinkns, "href");//remove the href attribute from the link

My question is : what is the correct syntax to do exactly the same thing with javascript or snap SVG?

Upvotes: 0

Views: 2222

Answers (1)

Robert Longson
Robert Longson

Reputation: 124029

In snap it's

element.attr("xlink:href", "http://google.com");

Snap will figure out the namespace for you

In Ecmacript (which is a synonym for javascript) its

var xlinkns = "http://www.w3.org/1999/xlink";
var myLink = document.getElementById("link");
myLink.setAttributeNS(xlinkns, "href", "http://google.com");
alert(myLink.getAttributeNS(xlinkns, "href"));
<svg><a id="link" xlink:href="http://stackoverflow.com"/></svg>

Upvotes: 1

Related Questions