Click here to Skip to main content
15,888,579 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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;
    }


What I have tried:

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()?
Posted
Updated 8-Jan-19 4:09am

Not clear what the issue are but the posted JavaScript seem functioning in it current form with a minor tweak. Based on the markup code. I'm guessing the dynamic elements were missing the onkeyup event. and the id should be "add...", based on your example instead of "n..."

JavaScript
// find elements
  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" onkeyup="sync()" id="add'+ i +'" />');
    }

    function sync()
    {
     var n1 = document.getElementById('add0');
     var n2 = document.getElementById('add1');
     n2.value = n1.value;
    }


Example: cp_textbox - JSFiddle[^]
 
Share this answer
 
Comments
Maciej Los 8-Jan-19 11:22am    
I guess, you're right!
TARS166 9-Jan-19 0:46am    
Hi. This is working fine in the fiddle, but when used in the program, the output is not getting displayed in the area :(
TARS166 9-Jan-19 1:40am    
Hi. I get the issue now. The text boxes are already in the loop. Is there a way just to create the 'id' in the loop and append it in the textboxes?
Bryian Tan 9-Jan-19 19:10pm    
I think your code already doing that by assigning id in he loop
I simply create the text-boxes with the name/id/event-handler built in at the time of generation:

Something like this, in a most generic way:
PHP
<?php  
 function addElement($i) {
    $elemID = "elem$i";
    return "<element name='$elemID' id='$elemID'
             onEvent='eventHandler(\"$elemID\")'></element>";
 } // function addElement($i)
?>



And call this type of function to generate your element, with various values for $i (such as sequential integers).

Remember, the function is a template - you need to modify it to your needs. an <input> element, for example, would not have a closing tag.

Note, also, that I didn't break the php up into a lot of small script intermixed with the HTML. Often that makes it just too difficult to read and thus buggy.

 
Share this answer
 
v3
Comments
Maciej Los 8-Jan-19 11:20am    
Smart!
TARS166 9-Jan-19 0:57am    
Hi. I'm having a problem calling the function to generate the output. Can you kindly guide me on how to do it?
W Balboos, GHB 9-Jan-19 6:06am    
It returns a string - ECHO it

If you've just been cutting/pasting and don't really know how to do this I suggest you try: HERE

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900