Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

js get selection start from contenteditable

/**
  * @param {object} element HTMLElement
  */
function getCaretIndex(element) {
  let position = 0;
  const isSupported = typeof window.getSelection !== "undefined";
  if (isSupported) {
    const selection = window.getSelection();
    if (selection.rangeCount !== 0) {
      const range = window.getSelection().getRangeAt(0);
      const preCaretRange = range.cloneRange();
      preCaretRange.selectNodeContents(element);
      preCaretRange.setEnd(range.endContainer, range.endOffset);
      position = preCaretRange.toString().length;
    }
  }
  return position + 1;
}
Comment

PREVIOUS NEXT
Code Example
Javascript :: Codewars Beginner - Reduce but Grow 
Javascript :: sort object by value javascript 
Javascript :: is(:checked 
Javascript :: how can we redirect to third party page in angular 
Javascript :: import formik 
Javascript :: npm ERR! Missing script: "eject" react native 
Javascript :: javascript update url without reload 
Javascript :: expo create react native app 
Javascript :: write files in node js 
Javascript :: get current scroll height javascript 
Javascript :: canvas fillrect 
Javascript :: regex password validation 
Javascript :: initialize json array 
Javascript :: how to draw horizontal line in canvas 
Javascript :: Uncaught Error: "arc" is not a registered element. 
Javascript :: javascript sort alphabetically 
Javascript :: javascript element edit value 
Javascript :: jquery on 2 events 
Javascript :: activate treeview menu in adminlte 3.0.2 treeview-menu open 
Javascript :: Material App debug mode 
Javascript :: how to get the data attached with an element in javascript 
Javascript :: set value of radio button jquery 
Javascript :: js change div content 
Javascript :: log arguments in javascript 
Javascript :: change info pagination datatable 
Javascript :: breaking from a labeled while loop js 
Javascript :: console message inspector javascript 
Javascript :: remove floating point javascript 
Javascript :: how to sort a populated data in mongoose 
Javascript :: javascript enumerate 
ADD CONTENT
Topic
Content
Source link
Name
3+6 =