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

108
Visualizações
Replace mutation target text node with a span element

I am using MutationObserver to track character changes in my rich text editor. Everytime a text is edited, the mutation callback method is called with the list of mutation records. The mutation target is the text node in which changes are made. I want to wrap this text node with a span so that I can use some css to highlight changed text. However, this is not working for me. Can you please guide what am I doing wrong here?

function callback(mutations) {
    mutations.forEach(function(mutation){
        if(mutation.type == 'characterData'){
           let parentNode = mutation.target.parentElement;

           // Create the new node to insert
           let newNode = document.createElement("span");
           newNode.classList.add('change-highlighter');

           // appending existing text node to new span element
           newNode.appendChild(mutation.target);

           // inserting newNode before text node
           parentNode.insertBefore(newNode, mutation.target);         
        }
    });
  }
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

I would insert the span, then append the target.

function callback(mutations) {
    mutations.forEach(function(mutation){
        if(mutation.type == 'characterData'){
           let parentNode = mutation.target.parentElement;

           // Create the new node to insert
           let newNode = document.createElement("span");

           newNode.classList.add('change-highlighter');

           // inserting newNode before text node
           parentNode.insertBefore(newNode, mutation.target);

           // appending existing text node to new span element
           newNode.appendChild(mutation.target);
        }
    });
  }
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