4
4
4eloBek2015-08-26 13:31:22
JavaScript
4eloBek, 2015-08-26 13:31:22

How to determine the number of DOM elements to the left of the caret in a div?

How to determine the number of DOM elements to the left of the active caret in ? I searched everything already ... Thanks in advance. <div contenteditable="true"></div>

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Deleted Deleted, 2015-08-26
@DailyFuckups

Well, I have a slightly complicated method: you can try to iterate through the elements through a loop. As far as I know, JQuery and just JS do not seem to have fast functions for this.

D
Danila Simonov, 2015-08-30
@ElKornacio

window.getSelection().focusNode is the current element where the caret is set. Further, all options boil down to getting all the children of the contenteditable before this element. How to determine this is up to you, I would just compare the indices.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question