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

134
Visualizações
How to add/update text of elements created with insertAdjacentHTML method?

I am trying to create p tags and inside them span with insertAdjacentHTML method and give each one of them unique id, and after that I want to change or update the textContent, but I don't know the reason why it is not working?. If you have the solution please help me.

const wraper = document.querySelector("#wraper")
const place2 = "afterbegin";
const textOfTimerTile = `
<div class="dataWraper">
<p id="program"><span id="programData"></span></p>
<p id="machineId"><span id="machineIdData"></span></p>
</div>
`;
wraper.insertAdjacentHTML(place2, textOfTimerTile);

const program = document.getElementById("program");
const programData = document.getElementById("programData");
const machineId = document.getElementById("machineId");
const machineIdData = document.getElementById("machineIdData");

program.textContent = "Program";
programData.textContent = "Program Span";
machineId.textContent= "Machine ID";
machineIdData.textContent= "Machine Span";
console.log("p tag ", program);
console.log("span ", programData)
#program, #machineId{
width: 150px;
height: 100px;
background-color: green
}
#programData, #machineIdData{
width:100px;
height: 60px;
background-color: red;
}
<div id="wraper"></div>

about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

When you run program.textContent = "Program"; you're actually removing the span from the page (you can see that in the browser's dev tools).

What you can do instead is program.insertAdjacentText('afterbegin', 'Program')

about 4 years ago · Juan Pablo Isaza Relatório

0

According to MDN textContent:

Both textContent and innerText remove child nodes when altered. It is impossible to insert the nodes again into any other element or into the same element after doing so.

Meaning you override and inner elements of your <p>'s. If you want, I suggest use innerHTML and add to it text instead of destroying your html structure with textContent.

However, since you set a new innerHTML you need to get the inner elements again.

To sum up -

const wraper = document.querySelector("#wraper")
const place2 = "afterbegin";
const textOfTimerTile = `
<div class="dataWraper">
<p id="program"><span id="programData"></span></p>
<p id="machineId"><span id="machineIdData"></span></p>
</div>
`;
wraper.insertAdjacentHTML(place2, textOfTimerTile);

const program = document.getElementById("program");
const machineId = document.getElementById("machineId");


program.innerHTML = "Program" + program.innerHTML;
const programData = document.getElementById("programData");
programData.innerHTML = "Program Span";

machineId.innerHTML = "Machine ID" + machineId.innerHTML;
const machineIdData = document.getElementById("machineIdData");
machineIdData.innerHTML = "Machine Span";
#program,
#machineId {
  width: 150px;
  height: 100px;
  background-color: green
}

#programData,
#machineIdData {
  width: 500px;
  height: 60px;
  background-color: red;
}
<div id="wraper"></div>

about 4 years ago · Juan Pablo Isaza Relatório

0

Now, I don't know that complete context for your code, but an alternative could be to make use of the template literal (template string) that you already defined.

const wraper = document.querySelector("#wraper");

let programtext = "Program";
let programDatatext = "Program Span";
let machineIdtext = "Machine ID";
let machineIdDatatext = "Machine Span";

const textOfTimerTile = `<div class="dataWraper">
<p id="program">${programtext}<span id="programData">${programDatatext}</span></p>
<p id="machineId">${machineIdtext}<span id="machineIdData">${machineIdDatatext}</span></p>
</div>`;

wraper.innerHTML = textOfTimerTile;
#program,
#machineId {
  width: 150px;
  height: 100px;
  background-color: green
}

#programData,
#machineIdData {
  width: 100px;
  height: 60px;
  background-color: red;
}
<div id="wraper"></div>

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