I want to create a rectangle inside an HTML page, then write some text on that rectangle. I also need that text to be a hyperlink. This is what I did but it is not working:
<!DOCTYPE html>
<html>
<body>
<script>
var svg = document.documentElement;
var svgNS = svg.namespaceURI;
var rect = document.createElementNS(svgNS,'rect');
rect.setAttribute('x',5);
rect.setAttribute('y',5);
rect.setAttribute('width',500);
rect.setAttribute('height',500);
rect.setAttribute('fill','#95B3D7');
svg.appendChild(rect);
document.body.appendChild(svg);
var h=document.createElement('a');
var t=document.createTextNode('Hello World');
h.appendChild(t);
document.body.appendChild(h);
</script>
</body>
</html>
Can you help please? Thanks.
Add this to html:
Try this function and adapt for you program:
To facilitate svg editing you can use an intermediate function:
Now you can write:
Example (with an improved getNode function allowing camelcase for property with dash, eg strokeWidth > stroke-width):
Change
to
so that you create a
SVG
element.For the link to be an hyperlink, simply add a
href
attribute :Demonstration