Document not ready for insert after method using loop with jquery

Is there a way to use a loop to insert individual DIVs one after the other?

Right now I'm using a loop for this, but it doesn't remember the inserted DIV before with the load method. As a result, I only see the last one.

The code is for displaying all messages after it reads the cookie with db-id when you first arrive on the page.

Summary:

the function addmessage()

gets the xml object and starts looping through it. Each individual post calls a function newnote()

that loads the DIV element and does stuff in its callback function like customization db-id

. After that, it returns to the function addmessage

.

0


a source to share


3 answers


What method are you using to insert the DIV? If you just want them all appended to the end of whatever container you use, you can just use append

. If you need them in a specific location, I would use before

orafter

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

      

If you need to insert something after itemTwo:



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

      

If you do this in a loop and must keep adding them sequentially after itemTwo (for example), you can try either keeping a link to the last inserted item and then using a function after

on that, or inserting it before the next item, itemThree.

+1


a source


Assuming your problem is finding the last DIV, you can find the last DIV and just put it after that.



0


a source


I found a solution, also with help from above, with a suggestion to leave a link

If I try to count the number of new items added with a selector like this

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

      

the result is always 0

BUT, if you explicitly provide the index number of the newnote () function, it works as expected. I just added a loop counter and used it as an additional argument to the function

The selector cannot count newly added elements within the function itself. At least that's my understanding of sofar.

0


a source







All Articles