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

121
Views
deleting a mark tag within contentEditable div creates a font tag after retyping

When I use within a <div contentEditable> a <mark> tag the text is marked as expected.

When I now remove the marked tag with Backspace and start retyping some new characters after deleted the last one inside the content editable there is a <font style="..."><span style="...">new text created.

<div id="editable" contentEditable>
  Here is some <mark>text</mark> for testing!
</div>

This DOM is created after deleting the word text and replaced it with new text.

<div id="editable" contenteditable="">
  Here is some <span style="background-color: rgb(255, 255, 0);">new text</span>&nbsp;for testing!
</div>

How can I make the contentEditable to

  1. create the same tag as deleted or
  2. create no tag at all

when retyping?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

I fixed it by calling the below function in the delete event by passing the HTML element to the function.

the calling:

placeCaretAtEnd(HTML_ELEMENT_OF_THE_CONTENT_EDITABLE);

the function:

placeCaretAtEnd(el) {
    el.focus();
    if (typeof window.getSelection != "undefined"
      && typeof document.createRange != "undefined") {
      var range = document.createRange();
      range.selectNodeContents(el);
      range.collapse(false);
      var sel = window.getSelection();
      sel.removeAllRanges();
      sel.addRange(range);
    }
  }
about 4 years ago · Juan Pablo Isaza Report
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!