Two Laptop Bag*
The moose likes HTML, CSS and JavaScript and the fly likes Changing the text inside the first TextBox , the last div gets updated Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Bookmark "Changing the text inside the first TextBox , the last div gets updated " Watch "Changing the text inside the first TextBox , the last div gets updated " New topic
Author

Changing the text inside the first TextBox , the last div gets updated

Roshan Khandelwal
Ranch Hand

Joined: Apr 16, 2009
Posts: 35
The issue is
1) On a WebPage using Javascript , dynamically creating elements .
2) The page initially has a parent Division
3) The code loops 10 times .

4) In each loop , the code creates one new input Box with a new Id [ inputid + i (loop counter) ] and add it to a parent Division .

5) A new division is created , with a new id [ id + i ] and add it to the parent division , with the innerHTML value of id + i .

6) Add a function to the text box which on change updates the innerHTML value of coressponding div [ or at least is expected to ]


However , with this code , when i change the text inside the first TextBox [ or for that reason any other textBox ] , the last div gets updated .

My understanding was that the inner div would be attached to the correct textBox . So changing the first text would update the first div and so on .
Please help .

Entire Code attached for reference :



Pretty sure , someone else would have faced the same issue . If someone could point to the right thread .
Eric Pascarello
author
Rancher

Joined: Nov 08, 2001
Posts: 15376
    
    6
Look at the The Infamous Loop Problem section: http://robertnyman.com/2008/10/09/explaining-javascript-scope-and-closures/

Eric
Roshan Khandelwal
Ranch Hand

Joined: Apr 16, 2009
Posts: 35
Great Link... Thanks a TON!!!

It not only solves my doubt but also is so much more enlighting . :)
 
wood burning stoves
 
subject: Changing the text inside the first TextBox , the last div gets updated
 
Similar Threads
Dynamically created {input type=file}
Enter text into newly created dynamic textbox
creating image link
question about objects and events in javascript
dynamically adding textboxes