views:

500

answers:

3

Is there a way to use a loop to insert separate DIV elements behind each other?

Right now I use a loop to do just that, but it doesn't remember the DIV inserted before with the load method. The result is that I only see the last one.

The code is meant to show all messages, after it reads the cookie with the db-id when you first arrive at the page.

summary:

the addmessage() function receives the xml-object and starts looping through it. Each separate message calls the newnote() function, which loads a DIV element and does stuff in its callback function like setting the db-id. After that it returns to the addmessage function.

+1  A: 

What method are you using to insert the DIVs? If you just want them all added to the end of whatever container you're using, you could just using append. If you need them in a particular location, I'd use before or after

<div id="container">
    <div id="itemOne"></div>
    <div id="itemTwo"></div>
    <div id="itemThree"></div>
</div>

If you needed to insert something after itemTwo:

$('#itemTwo').after("<div>New node</div>");

If you're doing this in a loop and need to keep adding them sequentially after itemTwo (for example), then you could try either storing a reference to the last-inserted element, and then use the after function on that, or by inserting it before the following item, itemThree.

nickf
do you mean, storing it in a reference outside these functions
A: 

Assuming your problem is finding the last DIV, you can search for the last DIV and just place it after that.

nevets1219
the problem is that I already am using the after method in the callback off the load method in the newnote()functionThe load method loads one note from a seperate fileThe problem is that I have too loop this function, but I loose any reference too the last inserted note if I keep calling the newnote()function from the main function addmessageI had the problem before with loading one note, but that was solved by doing stuff in the callback off the loadmethod. Now I have to create more then one note in a loop.
A: 

I found the solution, also with help from above, with the suggestion off having a reference

If I try to count the number off newly added elements with the selector like this

index = $("div.paneltbnote").size();

the result is always 0

BUT, if you explicitly give the indexnumber to the newnote() function, it works like it is supposed too. I just added a counter to the loop and used that, as an extra argument for the function

The selector cannot count newly added elements within the function itself. At least, that is my understanding sofar.