Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

78
Visualizações
Creating a Range at a specific point

I tried to create a range, which leads to a specific point in the text, but this was very difficult with the ChildNodes, because you can't reach exactly one point in the text, but only for example the beginning or end of a ChildNode.
I there a way to reach the point between „te“ and „st“ (like putting the cursor between „te|st“) or is it possible to create a range independent of the childnodes?

Here is my Code:

function myFunction(el) {
    var textNode = el.childNodes[1];
    var range = document.createRange();
    range.selectNodeContents(el);
    range.setStart(textNode, 0);
    range.setEnd(textNode, 0);
    var sel = window.getSelection();
    sel.removeAllRanges();
    sel.addRange(range);
}
<!DOCTYPE html>
<html>
<body>

<div id="myText" contenteditable>test<br>test</div>

<button type="button" onclick='myFunction(document.getElementById("myText"))'>Go into Text</button>

</body>
</html>

Thank you for every help!

about 4 years ago · Juan Pablo Isaza
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda