function doFindStringAndMoveCaret(elem, text) {
function setCurrentCursorPosition(element, chars) {
element.focus()
if (chars >= 0) {
var selection = window.getSelection();
range = createRange(element.parentNode, {
count: chars
});
if (range) {
range.collapse(false);
selection.removeAllRanges();
selection.addRange(range);
}
}
function createRange(node, chars, range) {
if (!range) {
range = document.createRange()
range.selectNode(node);
range.setStart(node, 0);
}
if (chars.count === 0) {
range.setEnd(node, chars.count);
} else if (node && chars.count > 0) {
if (node.nodeType === Node.TEXT_NODE) {
if (node.textContent.length < chars.count) {
chars.count -= node.textContent.length;
} else {
range.setEnd(node, chars.count);
chars.count = 0;
}
} else {
for (var lp = 0; lp < node.childNodes.length; lp++) {
range = createRange(node.childNodes[lp], chars, range);
if (chars.count === 0) {
break;
}
}
}
}
return range;
}
}
setCurrentCursorPosition(elem, new RegExp(text).exec(elem.innerHTML).index - 1)
}
doFindStringAndMoveCaret(document.getElementById('mydiv'), 'peekabo')
<div id='mydiv' contenteditable='true'>Lorem ipsum dolor sit amet, consectetur adipiscing elit.
Sed eget libero sit <mark>amet</mark> and **peekabo** magna sagittis sagittis quis nec risus.
Pellentesque feugiat pharetra purus id pharetra.
</div>
This just finds the position of the string in the innerHTML
of the div
, subtracts 1
from that, and uses setCurrentCursorPosition
to focus the div
and move the cursor to there:
setCurrentCursorPosition(elem, new RegExp(text).exec(elem.innerHTML).index - 1)
The rest of the code in the function is modified and taken from https://stackoverflow.com/a/41034697/.
If you want to move the cursor to the start of the text, use
setCurrentCursorPosition(elem, new RegExp(text).exec(elem.innerHTML).index - text.length - 1)
instead.
function doFindStringAndMoveCaret(elem, text) {
function setCurrentCursorPosition(element, chars) {
element.focus()
if (chars >= 0) {
var selection = window.getSelection();
range = createRange(element.parentNode, {
count: chars
});
if (range) {
range.collapse(false);
selection.removeAllRanges();
selection.addRange(range);
}
}
function createRange(node, chars, range) {
if (!range) {
range = document.createRange()
range.selectNode(node);
range.setStart(node, 0);
}
if (chars.count === 0) {
range.setEnd(node, chars.count);
} else if (node && chars.count > 0) {
if (node.nodeType === Node.TEXT_NODE) {
if (node.textContent.length < chars.count) {
chars.count -= node.textContent.length;
} else {
range.setEnd(node, chars.count);
chars.count = 0;
}
} else {
for (var lp = 0; lp < node.childNodes.length; lp++) {
range = createRange(node.childNodes[lp], chars, range);
if (chars.count === 0) {
break;
}
}
}
}
return range;
}
}
setCurrentCursorPosition(elem, new RegExp(text).exec(elem.innerHTML).index - text.length - 1)
}
doFindStringAndMoveCaret(document.getElementById('mydiv'), 'peekabo')
<div id='mydiv' contenteditable='true'>Lorem ipsum dolor sit amet, consectetur adipiscing elit.
Sed eget libero sit <mark>amet</mark> and **peekabo** magna sagittis sagittis quis nec risus.
Pellentesque feugiat pharetra purus id pharetra.
</div>