Originally Posted by rnd me
I had to think about this for a min or two
hopefully the means it was a decent question
But either I'm not understanding, or you think I'm trying to do the opposite of what I'm trying to do.
What I need is to listen for actual changes, called by functions that I have no control over, so this doesn't help:
because what I need to know is when this happens:
Being that you've stuck with me thus far, perhaps I can take a minute to explain the actual circumstance...
On the page, the user clicks a button, a hidden div appears and is populated by ajax with a whole lot of other stuff, including another button. I want to put a listener on that button. But adding the listener to it when the user clicks the first button fails because the 2nd button doesn't exist yet.
I had a ray of hope with jQuery's live() because it adds listeners to dynamically created elements. But it also adds them at the end (I'm guessing that this is in keeping with LIFO), and the point of adding the listener was to validate a form, so what was happening was that the form was submitted, then the validation code was run. Again, too late.
So this was the plan - listen for when the div display turns to "block", then add the listener because by that time the 2nd button exists. But this brought on the new problem - if the code notified me more than once, multiple listeners got set, meaning the validation code ran multiple times for one click of the button.
At the moment, what I'm seeing is that putting a setTimeout inside the click listener on the first button that puts the listener on the second button is surprisingly effective - in fact the timeout can be 0, because as I understand it (having read it somewhere) a timeout of any length guarantees that the function is placed at the end of the queue (or is this the stack? anyway...) which means that it won't be applied while the page is busy populating the div and once it is populated is exactly when I want the listener to be applied.
So that's my workable workaround at the moment. But I do recognize that it's kinda low-rent and would love to know if there is a good way to do this.