Change content of text elements in svg on a node.js server Change content of text elements in svg on a node.js server node.js node.js

Change content of text elements in svg on a node.js server


To your cheerio-edit, you can change the text-content of the nodes with

firstNameTag.text('This is the new first-name-text');lastNameTag.text('This is the new last-name-text');

And then get the string of the entire updated file with

const updatedSvg = $.html();


Side questions

When you import svg content from a file into your svg, it looks like this after import:

// svg from svgdom<svg>    <defs>...</defs>    // parser object, svg.js needs it    <svg></svg>    // imported svg    <svg></svg></svg>

With svg.get(2) you get back your svg instance. With the following get(8) you have your text element.

The SVG.Set you get back from select() is more or less a fancy array. svg.js has quite a good documentation at svgjs.dev where this is well explained. You can access elements of a set with get(index), first(), last().

Solution to the real question

However, what you are trying to achieve is quite easy. Just get the elements directly by ID:

SVG.get('first-name').text('New text')SVG.get('last-name').text('New text')