0

I have an understanding of what the .on function does as far "defining" functions/callbacks once an element is added to the DOM.

My question is, I want to automatically FIRE an event when the element is added to the DOM.

Here's the scenario: First off, I am using AJAX to load my pages, so DOM isReady() does not help me.

User clicks link and a function is fired off with a parameter that does:

  • create a series of images on a page; defines the onClick event for each image
  • I then look for one of those elements and performs click() event

Problem is, the second function fires too quickly. What I want to do is WAIT for the first function to complete entirely - i.e. wait for all the elements to be created, and THEN fire the click()

I see that the now retired(?) plugin LIVEQUERY used to do that.

Live Query also has the ability to fire a function (callback) when it matches a new element and another function (callback) for when an element is no longer matched.

Is there a way to do this this with the new version(s) of jQuery?

Again, I simply want to wait until an element is added to the DOM and then trigger an event on that element.

Any guidance is greatly appreciated.

2
  • There isn't a built-in jQuery way to do it, you would have to either extend jQuery and/or using DOMMutation Events. Commented Sep 17, 2012 at 20:28
  • could you post your callback function so we can see why is not working properly? Commented Sep 17, 2012 at 20:28

1 Answer 1

4

It's safe to assume that when the last image loads, everything else has too.

Try using the img's onload event. It'll be called when the image has finished loading which will be after your elements have been created.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.