I've experimented with various methods, but I'm unable to make it work. Is there a way to assign custom attributes to an SVG element? Here's the code snippet I'm using:
var svgns = "http://www.w3.org/2000/svg";
var rect = document.createElementNS(svgns,'rect');
rect.setAttributeNS(null, 'x', 0);
rect.setAttributeNS(null, 'y', 0);
rect.setAttributeNS(null, 'height', 20);
rect.setAttributeNS(null, 'width', 20);
rect.setAttributeNS(null, 'fill', 'blue');
rect.setAttributeNS(null, 'id', '999');
// My attempt here
rect.setAttributeNS(null, 'foo', 'bar');
rect.addEventListener('click',
function() {
alert(this.foo);
}
,false);
document.getElementById('yard').appendChild(rect);
When I click on the rect, I expect it to display the value of the attribute 'foo'. However, it currently shows undefined. Any ideas?