I am trying to insert text inside SVG using appendChild()
method. I need to use this method for the actual code I am writing.
In the attached code, you will find that I am trying to write the string "How are you?" inside the SVG using appendChild()
method. However it doesn't display. I checked the execution using Chrome developer tools, myTextElement
and myText
are actually executed in the code. I don't see why it is not being displayed, though. Can you please help?
var mysvg = document.getElementById("mysvg")
var myTextElement = document.createElement("text");
var myText = document.createTextNode("How are you?")
myTextElement.appendChild(myText);
mysvg.appendChild(myTextElement);
circle {
fill-opacity: 0.5;
stroke-width: 4;
fill: #3080d0;
stroke: #3080d0;
opacity: 0.6;
}
<svg id="mysvg" version="1.1" xmlns="http://www.w3.org/2000/svg" width="400" height="300">
<circle id="my-circle" cx="100" cy="100" r="50" />
<text x="0" y="10" font-family="Verdana" font-size="55" fill="blue"> Hello </text>
</svg>