Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

201
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!