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

123
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
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