Reputation: 1433
I have an 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 a JS file. I already tried but I'm 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);
Upvotes: 9
Views: 40482
Reputation: 508
For your specific example, here's how to set the text using both raw JavaScript and jQuery:
updateText("tspan5834", "A different score");
//updateTextWithJQuery("tspan5834", "now using jquery"); // uncomment this line to see how jquery works
function updateText(tspanId, txt) {
var spanEl = document.getElementById(tspanId);
while( spanEl.firstChild ) {
spanEl.removeChild( spanEl.firstChild );
}
spanEl.appendChild( document.createTextNode(txt) );
}
function updateTextWithJQuery(tspanId, txt) {
$("#"+tspanId).text(txt);
}
jsFiddle: https://jsfiddle.net/w91sn1dk/3/
Upvotes: 3
Reputation: 14591
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>
Upvotes: 3
Reputation: 10567
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>
Upvotes: 15