<svg width="200" height="100">
<text id="exampleText" x="10" y="40" font-size="16">Hello, SVG World!</text>
</svg>SVGTextContentElement: getCharNumAtPosition() method
Runnable notebook: every JavaScript, HTML and CSS example on this page executes in a sandboxed local frame. Nothing leaves the browser.
const textElement = document.getElementById("exampleText");
// Create a DOMPoint for the position (30, 40)
const point = new DOMPoint(30, 40);
// Get the character at the specified position
const charIndex = textElement.getCharNumAtPosition(point);
console.log(charIndex); // Output: 2 (for character "l")
// Check with a point where no character is present
const offPoint = new DOMPoint(300, 40);
const offCharIndex = textElement.getCharNumAtPosition(offPoint);
console.log(offCharIndex); // Output: -1 (no character found)