Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dynamically change text of SVG file

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);
like image 541
Mukesh Gupta Avatar asked Feb 09 '16 09:02

Mukesh Gupta


People also ask

How do I make SVG text editable?

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"> .

How to append SVG in JavaScript?

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.

Is SVG dynamic?

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.


2 Answers

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>
like image 92
Sooraj Avatar answered Oct 10 '22 09:10

Sooraj


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>
like image 3
Gilsha Avatar answered Oct 10 '22 10:10

Gilsha