Jaso2970
Jaso2970

Reputation: 147

How to fire an jquery action when a div is appended?

I need to fire an jquery action when a div is appended.

Something like:

if ($('div#myDiv').is_appended()) {
console.log('the div has been appended');
}

Is this possible?

Why do I need this

This question is related to this question where I have I modal div which is loaded after the jquery library, then I can't target the element in it.

Now I'm trying to do something when the modal div is load, not sure if I get it done though,

Upvotes: 0

Views: 58

Answers (1)

T.J. Crowder
T.J. Crowder

Reputation: 1075199

Not in a cross-browser way, not yet. There is a draft specification for mutation observers which would allow it (more on this MDN page), but it's only implemented in recent WebKit-based browsers and Firefox.

There's almost always a better approach available, such as triggering the action from the code appending the div. In the worst case (and I mean "worst"), you could use polling every (say) 100ms and detect the new div. But again, there's almost always a better way.

Upvotes: 2

Related Questions