1

Lets say I have a contenteditable div which also has some element in it

<div id="text" contenteditable="true"><b>abc</b><nav>def</nav><span>ghi</span></div>

Now if the cursor or caret position is within the span element, how do I get the index of the span element as a child node to its parent element div. So I can have this result

divNode = document.querySelector('div');
spanIndex = //get the index of span element which will be = 2
caretEle = divNode.childNodes[spanIndex];

NOTE: I am not looking for the caret position, I want to get the child index of the caret position element to its parent element. ie I want to get the child index of the <span> element to the <div id="text" contenteditable="true">

For example:

node = document.getElementById('text');
node.childNodes[0] == <b>
node.childNodes[1] == <nav>
node.childNodes[2] == <span>
doggie brezy
  • 289
  • 3
  • 16

0 Answers0