user3266510
user3266510

Reputation: 81

Add new text to svg

I create a svg area. As things happen in the document, I want to display text strings in the svg area in xy coordinates within the svg area where I click.

How can I append a new text element to given xy coordinates in the svg area as these things happen?

Upvotes: 0

Views: 62

Answers (1)

Paul LeBeau
Paul LeBeau

Reputation: 101820

var  svg = document.getElementById("id-of-my-svg");

var  text = document.createElementNS("http://www.w3.org/2000/svg", "text");
text.setAttribute("x", clickX);
text.setAttribute("y", clickY);
text.appendChild( document.createTextNode("Some text") );

svg.appendChild(text);

Demo here

Upvotes: 1

Related Questions