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

182
Visualizações
Set range to select text accross multiple child nodes

I have some issue to set range to select text within a div.

Here is a fiddle I use for the test.

I succeed do it within first child only using node.firstChild:

  let selection = window.getSelection();
  var node = document.getElementById("textZone");
  var textZone = node.firstChild;
  selection.removeAllRanges();
  range = document.createRange();
  range.setStart(textZone, 0);
  range.setEnd(textZone, 2);
  selection.addRange(range);

But where I only use node (as per the fiddle), the value of setStart and setEnd will defined the child number of the node instead of the position value of the first and last letter position.

My goal is to be able to proceed selection accross the entire text based on caracter position value. For example, selection should be:

  • middle of node child 2 to middle of node child 4

The result to achieve is, when I press the button, I get that selection : enter image description here

Please let me know if you have any ideas.

Thank you

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

I am not sure what exactly you want to achieve but your approach will not work like this. If you want just to extract the plain text than just do range.toString(); and apply some string functions to get your result.

The problem with document.createRange() is the fact that it works with nodes and what you really want is just a part of a node. Imagine that your node is the parent for others like: <strong>ins<i>is</i>t</strong> .... You will have somehow to split the node and create new nodes with the remaining parts and another nod with the wanted part. So your <strong>insist</strong> should look like

<strong>ins</strong><strong>ist</strong>
about 4 years ago · Juan Pablo Isaza Relatório
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