tags:

views:

591

answers:

2

I need to dynamically append a set of elements to a div via jQuery and then turn around and reference them. It's really odd that you can't do this, since if you view the DOM in firebug, the elements are there. jQuery just doesn't act like they exist.

HTML...

<div id="images"></div>

This gives me my elements added to the <div>...

     $.getJSON("http://localhost/wordpress/test.php",
  function(data){
          $.each(data, function(i,item){
            $("<img/>").attr("src", item).appendTo("#images");
          });
        });

Then I immediately try to select and use the images...

  $("img").each(
   function(i,item) {
    alert(item);
   });

jQuery doesn't select any of the newly added elements. It basically acts like none of the images were added.

+3  A: 

I would hazard a guess that when you run the second block of code the images aren't there yet. The getJSON call is asynchronous. This means you don't know when it will return and add the image elements to the page.

Have you tried adding your second block of code in the same function that calls the appendTo? This is a common source of confusion when people do asynchronous code.

Jeremy Wall
I think you're right, his second snippet is executing before the images are appended. Good call.
Mark Hurd
Thanks, If I had a nickel for every time I made this mistake early in my javascripting career I'd be able to retire.
Jeremy Wall
+3  A: 

You need to use jQuery's live function since your new images are being inserted into the DOM after page load.

As a quick example:

$("img").live("click", function() {
    alert(this);
});

Will trigger an alert on every image on the page, regardless of when it was added. Your typical $("img").click() would not have worked on your appended images.

Mark Hurd
+1 Although it looks like the O.P. isn't doing any event binding, just some selection, you raise an excellent point as this is often another fine culprit!
Funka
So, if I'm reading correctly, "live" DOES NOT help me with regard to simply selecting the elements of the DOM that I've created dynamically? Funka, you are right:1. Create Elements Based on Asynch Get to JSON2. Select the Newly Created ElementsThere is no event binding going on.
Eric Hendrickson
After re-reading your post, I'm inclined to agree with Jeremy Wall now. Try moving your second snippet of code into the getJSON function that is initially appending the images. Your selector is trying to access elements that are not yet in place. Moving it to the getJSON function will cause it to only execute AFTER the append is complete.
Mark Hurd
Yes, Eric, you are reading correctly: "live" does not help you in this particular situation. However, had you been trying to bind an event such as an image click handler to all images --- for example: $('img').click(myFunc); --- and had you found that your event was only firing on the original images but not on any newly added images, then at that time you would want to refer back to this post! ;-) Best of luck! -Mike
Funka