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

200
Visualizações
How to add dynamic id to text boxes and fetch its value?

I'm trying to fetch value of each textbox keyup event inside for loop.And after the series of text boxes are created, I want to call keyup event, which fetch the value entered in each text box

what i have tried

script for assigning dynamic id

<textarea type="text" name="add'+ $i +'" id="add'+ $i +'"
value="" class="form-control" onkeyup="callans()" ></textarea> <br/>

function callans() {
for( var i=0; i<8; i++) 
   //i<9 because that's the maximum number of text 
  //fields to be created is 8.     
    {        




<textarea type="text" name="add'+ $i +'" id="add'+ $i +'"
value="" class="form-control" onkeyup="callans()" ></textarea> 

  var elements = document.getElementById("add'+ $i +'").value;

       alert(elements); 


    }
}

Is there a way only create the id's dynamically and append it in the input area?

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

0

Without using ID attributes you can very easily identify elements in the DOM by inspecting the event - in your case you are interested in the keyup event and to make life easier by using a delegated event listener bound to the input elements common parent we can obtain the values like so:

(vanilla js mainly )

for( var i=0; i<8; i++){        
    $('#addition').append('<input class="form-control" type="text" name="add" />');
}

// delegated event listener bound to the parent container but processing only `INPUT` elements of type `text`
document.querySelector('#addition').addEventListener('keyup',function(e){
  if( e.target.tagName=='INPUT' && e.target.type=='text' ){
    console.log(e.target.value)
  }
});
<script src="//cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<div id='addition'></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