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

144
Visualizações
Add span for specific index without get the events deleted

i am trying to add spans to specific index in innerHtml of page

when i replace the old innerHtml with the new one > all the events in the page don't work

then i realize that the innerHtml make the events don't work >>

so how i can make this operation without loosing the events in the page ?

here is my code : -


var startInd = [50]; //the index of word in innerHtml

var lastInd = [55];  //the last index of word in innerHtml(its length)(the spanned area)

var str = document.body.innerHTML;

var count = 1;


for (let j = 0; j < count; j++) // count always 1
{
    let pre = str.substring(0, startInd[j]); // all html elements before the word
    let post = str.substring(lastInd[j], str.length); // all html elements after the word
    let phrase = str.substring(startInd[j], lastInd[j]); // the word

    let nextPhrase;

    if (j < count - 1) 
    {
        nextPhrase = str.substring(startInd[j + 1], lastInd[j + 1]);
    }

    str = pre + `<span id="myHeader${i}">${phrase}</span>` + post; // make the new innerHtml
    newID = "myHeader" + i;

    if (j < count - 1) 
    {
        startInd[j + 1] = str.indexOf(nextPhrase, startInd[j + 1]) - 1;
        lastInd[j + 1] = startInd[j + 1] + nextPhrase.length;
    }

    document.body.innerHTML = str
}

about 4 years ago · Juan Pablo Isaza
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