I am trying to get svg path string out of svg path in html. Ler's suppose I have a svg Path in this format.
<path d="M 0 68.5 C -1.56977e-13 30.6685 184.011 -1.30814e-14 411 -3.92443e-14 C 637.989 -5.23258e-14 822 30.6685 822 68.5 C 822 106.332 637.989 137 411 137 C 184.011 137 -1.56977e-13 106.332 0 68.5 Z" fill-rule="evenodd" fill="none" stroke="none"></path>
How can I get the value inside path tag, specially value inside d attribute.
There is one way to get it by doing some basic operation like getting index of M and fill, but that is not a very good solution for this.
var htmlStr = elem.outerHTML;
var start = htmlStr.indexOf("M");
var end = htmlStr.indexOf("fill");
var str = htmlStr.substring(start, end);
str.replace("\"", "");
str.trim();
Can anybody suggest some generic solution for this?
Note: path tag does not have any Id, and I can't create one as it is coming from other service.
You can juste use the attributes of path like that :
const pathId = document.getElementById("pathId");
const dValue = pathId.attributes["d"].value;
console.log(dValue);
<path id="pathId" d="M 0 68.5 C -1.56977e-13 30.6685 184.011 -1.30814e-14 411 -3.92443e-14 C 637.989 -5.23258e-14 822 30.6685 822 68.5 C 822 106.332 637.989 137 411 137 C 184.011 137 -1.56977e-13 106.332 0 68.5 Z" fill-rule="evenodd" fill="none" stroke="none"></path>