Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

186
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda