I have external SVG file which contains the code
<g
inkscape:groupmode="layer"
id="layer9"
inkscape:label="score"
style="display:inline">
<text
xml:space="preserve"
style="font-style:normal;font-weight:normal;font-size:22.5px;line-height:125%;font-family:sans-serif;letter-spacing:0px;word-spacing:0px;fill:#000000;fill-opacity:1;stroke:none;stroke-width:1px;stroke-linecap:butt;stroke-linejoin:miter;stroke-opacity:1"
x="100.3568906"
y="20.353357"
id="text5833"
sodipodi:linespacing="125%"><tspan
sodipodi:role="line"
id="tspan5834"
x="300.3568906"
y="20.353357">Score</tspan></text>
</g>
I need to dynamically change the text Score from JS file, i already tried but not able to change the text dynamically. what i tried is:-
var list = layerNamed('score').getElementsByTagName("g");
var textNode = document.createTextNode("Score:-1");
list.appendChild(textNode);
But most importantly, what's the best way to make SVG text editable? document. getElementById("element"). contentEditable = "true"; You can also use the ref contenteditable="true" in an HTML element like so: <div contenteditable="true"> .
To add SVG elements to existing SVG using DOM manipulation and JavaScript, we can select the svg element. Then we can create an element with a namespace and put it inside the svg element as its child. Then we can select the element and add a path element into it by writing: const svg = document.
As the images are defined with code, you can CONCATENATE() strings of code together while including application variables, to make the images dynamic. In this way, you could have animated images which react to user input.
You can use the following code to change the text inside existing text
element.
document.getElementById('textid').textContent = "new text";
Working example below:
function changeText()
{
document.getElementById('textid').textContent = "new text";
}
<svg height="30" width="200">
<text id="textid" x="0" y="15" fill="red">I love SVG!</text>
Sorry, your browser does not support inline SVG.
</svg>
<button onclick="changeText()">Click here to change text</button>
There are a few problems in your code.
1) layerNamed('score').getElementsByTagName("g")
returns a collection of all elements with the specified tag name, as a NodeList object. The nodes can be accessed by index numbers. The index starts at 0.
So to access the first g element, you should code as shown below.
var list = layerNamed('score').getElementsByTagName("g")[0];
2) There is no need for appending a new text element for updating the text content. Just access the text and tspan and update the text content of tspan.
var textNode = list.getElementsByTagName("text")[0].children[0];
textNode.textContent = "New Text";
var list = document.getElementsByTagName("g")[0]; //document.getElementById("#layer9");
var textNode = list.getElementsByTagName("text")[0].children[0];
textNode.textContent = "New Text";
<svg height="30" width="800">
<g inkscape:groupmode="layer" id="layer9" inkscape:label="score" style="display:inline">
<text xml:space="preserve" style="font-style:normal;font-weight:normal;font-size:22.5px;line-height:125%;font-family:sans-serif;letter-spacing:0px;word-spacing:0px;fill:#000000;fill-opacity:1;stroke:none;stroke-width:1px;stroke-linecap:butt;stroke-linejoin:miter;stroke-opacity:1" x="100.3568906" y="20.353357" id="text5833" sodipodi:linespacing="125%"><tspan sodipodi:role="line" id="tspan5834" x="300.3568906" y="20.353357">Hello</tspan></text>
</g>
</svg>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With