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

152
Views
range.deleteContents() is unexpectedly removing whitespace before the range itself

if the text is sure :f, range.deleteContents() suppose to delete :f, but, it's deleting :f. It looks like space before : character is also getting removed.
How to retain the space before :? Alternatively, Is there any other way to remove text which is more reliable?

const selection = window.getSelection();
  const range = document.createRange();
range.setStart(selection.focusNode, 5); // start of :
  range.setEnd(selection.focusNode, 7);
  range.deleteContents(); //deletes ` :f`

It doesn't matter if I set range.setStart(selection.focusNode, 4) or range.setStart(selection.focusNode, 5).. space is getting removed.

Here's the fiddle to play around: https://jsfiddle.net/JerryGoyal/o2c9f5gw/

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!