Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

234
Views
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
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!