views:

149

answers:

3

I hope I can find a solution to this one. Here it goes:

After I have submitted my form to add a message to the database, I create a <div> with the load method. After that I do some stuff in the callback function.

When the function to create the new <div> has finished, it returns to the calling function that is supposed to prepend the message to the last inserted <div>.

That's where the problem starts:

I use the selector to find $(someDiv : last), but it puts it in the wrong <div>. So, I know it has something to do with the async process.

I haven't really worked out what I could do to fix this, because now I am not working from a callback function.

If you wanna see a live example The sourcecode is clutterd with debug code in the form of alerts, but you can see what it is doing.

A: 

Probably the new div is not being inserted where you think it is. $(someDiv : last) is returning the wrong result because the new div isn't in someDiv, or it isn't at the end. This seems like the kind of thing that Firebug would make easy to debug.

Rather than using the "last" metaclass to select the div, here's a better idea: give the new div an ID and refer to it directly. Or give it a class when you create it, use the class to select it $(".newdiv"), and then clear the class when you're done. Or simply return the new div to the calling function, so it doesn't need to use a selector at all. Sorry, I didn't entirely understand your situation, but I think at least one of these solutions will work.

redmoskito
I understand what you are saying. I used a class in the selector.What I do know, is that I have no problem finding the div within the callback of the load method with the pseudo selector :last. Maby, it is like you said, that I should use the specific id of the div or pass it along. I have not try'd that yet.
A: 

Did you notice that the first time the value gets added to the first item, and after that it gets added to the previously last item? That might be a clue. Oh, and I got stuck in a loop which added my message 10 times. Looks like your code has timing issues.

Edit: bringing it back to its basics would look something like this: http://kruisit.nl/so/841982/ Hope that helps.

Jeroen
I have not encountered that problem. Do you know how it can be solved?
and yes, thats due too the fact that the first div with that class is already on the page so the selector finds it second time around it finds the one that was created before that. It's got to do with that async stuff.
your example, does not do anything if you press the buttonAnyway, I put my stuff in the callback. This solves the problem for now.
You're using IE?
Jeroen
yes, I doI also noticed that events like mouseleave don't work for meI have too use hover out
+1  A: 

Ajax calls are asynchronous, you should be using a callback function to select loaded data, for example:

$('div').load(url, data, function(data, status) {
   $(someDiv:last).dosomething();
}
Nadia Alramli
ok, but then I would have to change the logic and put it all in one function. Right now I submit a form -> return xml object to addmessage -> admessage decides to make a new div -> newdiv() returns to addmessage()-> addmessage wants too put message into the new div.
Then You are already suggesting, that there is no other solution?
I already used this callback in the newDiv function see below What are the data, status arguments doing within that function?$('.wrappertbnote:last').load('tbnote.html .paneltbnote', functio(){ $(".paneltbnote:last").prepend("testmessage");
I think I am going to pass the messages to the function wich in turn the callback function can use to prepend the message too the div. That is also not too complicated
I'm glad you found a way around it
Nadia Alramli