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

170
Visualizações
How to make dynamic changes in HTML?

So I want to add to a variable every time someone clicks a button on my website. I am very new to HMTL so I don't know how to do this. All the examples I've googled just change text into other text and not adding to a variable.

If someone would like to enlighten me on how to do this I would greatly apricate it.

 
 function changeIt() {
    document.getElementById('test').innerHTML = "<h2>Congrats</h2>";
  }
<div id="test">
   <b> <var> Test </ var> </b>
</div> 

<button onclick="changeIt()">Test</button>

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

0

var sum = 0;

function changeIt() {
    sum++;
    document.getElementById('test').innerHTML = `<h2> ${sum} </h2>`;
    
  }
<div id="test">
   <b> <var> Test </ var> </b>
</div> 

<button onclick="changeIt()">Test</button>

about 4 years ago · Juan Pablo Isaza Relatório

0

The code you've written in document.innerHTML is correct. For making it dynamic and to add a new Congrats onto the screen instead of just modifying the old one, you need to keep a global counter and loop over it.

let count = 0;
function changeIt() {
   count++;
   for (let i=0;i<count;i++) {
      document.getElementById('test').innerHTML = '';
      let h2 = document.getElementById('test').createElement;
      h2.innerHTML = "Congrats";
      document.getElementById('test').appendChild(h2);
   }
}

Since you are calling the changeIt function on every button click it will let you update the count and will loop over the count to add the Congrats 'count' number of times to your 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