Skip to content

Instantly share code, notes, and snippets.

View al-dot-dev's full-sized avatar
🏠
Working from home

Alex Pavlov al-dot-dev

🏠
Working from home
View GitHub Profile
@al-dot-dev
al-dot-dev / contenteditable-caret-position.js
Created August 24, 2025 13:20 — forked from loilo/contenteditable-caret-position.js
Some utilities for detecting the caret position inside a contenteditable element
/**
* Get the number of characters in an element
*
* @param {Element} element
* @return {number}
*/
function getTextLength(element) {
let range = element.ownerDocument.createRange()
range.selectNodeContents(element)