SVG inherit all the core DOM methods to get and set attributes and styles.
There are a few complications : SVG is a namespaced XML language, so namespace-sensitive DOM methods when you’re creating elements or setting xlink attributes.
Creating elements named “svg” and “use” in an HTML documnet without setting a namespace, just creates HTMLUnknownElement objects with those names.
This code will work in devtools but will not irl.
Following code creates similar looking elements but with correct namespaces.
It’s not ideal, you have to hardcode XML namespace URLs in your code.
Alternatively, you can use the HTML parser from JS, by setting tyhe innerHTML property of an HTML element.
Be warned : this works only in an HTML document.
How to get your JavaScript into SVG ?
Inline SVG : any script running in your HTML file has access to your inline SVG elements. (inlude in scripttag in head or body as usual).
Standalone SVG : SVG has its own script element. Very similar to html scripttag but not the same.
To include an external JS file with an SVG script element, use an xlink:href attribute (not src like in HTML).
If you want to include the script internal, wrap it in an XML character data (CDATA) block, so that less than and greater than or ampersands, do not cause XML validation errors.