Как присвоить идентификатор динамически создаваемым текстовым полям?
I'm trying to display data in a loop of text fields from a value entered in another text box. The script used to work fine when the text fields were static. But when I looped through the text field, the data gets reflected only in the first text field, and rest of the fields remain blank, because the text box id is still static. The master text box where the data is entered. <input class="form-control" type="text" name="n1" id="n1" onkeyup="sync()"> Area with the dynamic text fields. <?php while($res = pg_fetch_assoc($result)){ ?> <div class="col-sm-2 show-hide"> <input type="text" value="<?php echo $res['size']; ?> " readonly style="background-color: #F5F5F5;" class="form-control"><br> ///Text field where data is supposed to be mirrored and the id is static. <input class="form-control" type="text" name="n2" id="n2"/> // <select class="form-control"> <option>25%</option> <option>50%</option> <option>100%</option> </select><br> </div><?php }?> Script function sync() { var n1 = document.getElementById('n1'); var n2 = document.getElementById('n2'); n2.value = n1.value; }
Что я уже пробовал:
This is what I tried doing to dynamically assign 'id' to all the text boxes being created in the loop. for( var i=0; i<9; i++) //i<9 because that's the maximum number of text //fields to be created is 8. { $('#addition').append('<input class="form-control" type="text" name="add" id="add'+ i +'" />'); } And display the data like this. <div id = "addition"></div> When I'm using this, nothing gets displayed. How do run it the loop and generate dynamic id's so that I can use it in the function sync()?