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

235
Visualizações
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 à 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