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

232
Vistas
Javascript range.collapse(false) position falling into following paragraph sometimes

I am struggling to work out how to manage the problem of double-clicking on a paragraph giving one type of range, vs selected the same paragraph giving a smaller range.

The issue being that if I double-click then the end container seems to be the following paragraph, but when selecting manually then the end container remains the same paragraph.

To demonstrate this I have a jsfiddle - https://jsfiddle.net/Abeeee/uagt0z28/39/ that uses insertText to inject text at a range collapse position;

var range0 = range.cloneRange();

range.collapse(true);
document.execCommand('insertText', false, '!!!START!!!');

sel.removeAllRanges();
sel.addRange(range0);
range0.collapse(false);
document.execCommand('insertText', false, '!!!END!!!');

the problem is found when you double-click on a word in a paragraph (which selects the whole paragraph), and then you click [setRangeText] to see where the code believes the start and end points of that selection are). double-click to select whole paragraph

What I want is that the paragraph is surrounded by the START and END markers to the left and right of the first and last words as seen here ...

manual selection test

... but what I get is the END marker sitting with the next paragraph (and inside the < p> tag for that para), as seen here...

Double-click test

How do I fix/correct for this?

about 4 years ago · Juan Pablo Isaza
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