Dexty
Dexty

Reputation: 1472

jQuery append, not exactly available?

When i've loaded a page, you'll be able to upload some photos. The images that i've uploaded is then appended (using .append) to a list-element. How can i assign my JS code to make those images clickable?

I've tried with $('img').click() etc but since the images is added after the DOM is ready i dont think they exactly is available.. how can i fix this?

Upvotes: 1

Views: 55

Answers (2)

Hristo
Hristo

Reputation: 46517

Check out the jQuery .live() handler

The .live() method is able to affect elements that have not yet been added to the DOM through the use of event delegation: a handler bound to an ancestor element is responsible for events that are triggered on its descendants. The handler passed to .live() is never bound to an element; instead, .live() binds a special handler to the root of the DOM tree.

Alternatively, you can check out the jQuery .delegate() handler and read this StackOverflow response on the differences between .live() and .delegate()...

Jquery live() vs delegate()

I hope this helps.

Upvotes: 1

rxmnnxfpvg
rxmnnxfpvg

Reputation: 31003

.live() was made for this:

$('img').live('click', function() {
      // code here
});

Upvotes: 5

Related Questions