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);
For your specific example, here's how to set the text using both raw JavaScript and jQuery:
jsFiddle: https://jsfiddle.net/w91sn1dk/3/
You can use the following code to change the text inside existing
text
element.document.getElementById('textid').textContent = "new text";
Working example below:
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.
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.