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

307
Visualizações
i want to create a counter which change every time i click on the button in javascript

a set a button which create an new div everytime i click on it , every div created must have a new i so i try to create an counter which change every time i click on the button i try a lot of codes but none work correctly , this is the last code i tried

function AddDiv() {
  var i = 0;
  $("#AddDrop").click(function() {
    i++;
  });
  const e = document.createElement('div');
  e.setAttribute('id', i);
  e.style.border = 'solid';
  e.style.width = '300px';
  e.style.height = '100px'
  e.innerHTML = 'Droppable'
  question = document.getElementById("QU");
  question.appendChild(e);
}

function GetText() {
  const event = document.createElement('div')
  const text = document.getElementById("in").value;
  event.innerHTML = text;
  question = document.getElementById("QU");
  question.appendChild(event);
}
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js"></script>
  <script src="https://code.jquery.com/jquery-1.10.2.js"></script>
  <script src="https://code.jquery.com/ui/1.10.4/jquery-ui.js"></script>
  <title>Document</title>
</head>

<body>
  <div class="row">
    <div class="col-md-6">
      <textarea id="in" style="width: 500px; height: 300px;"></textarea>
      <br>
      <button onclick="GetText();" class="btn btn-primary">Add Text</button>
      <button onclick="AddDiv();" class="btn btn-success" id="AddDrop">Add div</button>
      <input type="file" accept="image/*" value="Add Images" style="margin-top: 30px;">
    </div>
    <div class="col-md-6" id="QU" style="border: solid; height: 600px;">

    </div>
  </div>

</body>

</html>

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

0

In your AddDiv() function, you are always resetting i to 0, which means every time that you run the function, it's going to be one all over again. You need to take it out of the function to keep it counting.

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