Search code examples
javascriptsvgfield-description

How to access the description of a svg element in js?


svg elements can have descriptions as described here

https://developer.mozilla.org/en-US/docs/Web/SVG/Element/desc

Assuming I got the handle of the circle via getElementById, how can I access the content of the <desc> tag?

Adding an id to the tag cannot be a solution given the SVG comes from an editor that allows me to add id's in the elements, but not to their descriptions.

PS. It's not relevant, but the idea is to add javascript code in the descriptor to be executed (via Eval) to update the owner element.

Thanks!


Solution

  • assuming you've the circle then calling

    let desc = circle.getElementsByTagName("desc");
    

    will get you all the desc elements that are descentants of the circle element.

    You can loop over those and do what you want with them. Note that you'll get a collection of desc elements so if there's only one element it will be the first and only element in that collection.