1

Possible Duplicate:
How to detect new element creation in jQuery?

I have a page, which has several divs that are created by a different jQuery call. Once these divs are created, I want to make an ajax call to load some html into them. I have tried something like this (coffeescript):

$("#myDiv").ready ->
  alert "div ready"

but because the .ready function is only defined for a document, this runs immediately upon page load (before the div exists). I also tried this:

$("#splunk_results").ready ->
  $("#splunk_results").each ->
    alert "div ready"

as per the selected answer here, but this seems to not load at all.

Basically, is there a simple way to execute code when a div appears?

Community
  • 1
  • 1
maxko87
  • 2,892
  • 4
  • 28
  • 43
  • 1
    Can you have the same jQuery that adds the divs make the ajax call and then populate them? – scottm Jul 19 '12 at 20:35
  • I could, but that would make the code more confusing (this has to happen in several different place) and creating listeners would allow for nicer compartmentalization of code – maxko87 Jul 19 '12 at 20:37
  • can you make use of callback? http://docs.jquery.com/Tutorials:How_jQuery_Works#Callback_and_Functions each time you create a div call a function? – ant Jul 19 '12 at 20:37
  • or call the ajax and only create the div on successful ajax callback? – scottm Jul 19 '12 at 20:39
  • You could look at http://www.w3.org/TR/dom/#mutation-observers – Prusse Jul 19 '12 at 20:50
  • ant, what would the element be? $. what? – maxko87 Jul 19 '12 at 20:52

0 Answers0