1

Is it possible to call jquery function when a new element has been added to the DOM?

With jquery .on() I can do something like this:

<script type="text/javascript">
    $(document).on("click", ".my-test", function() {
        console.log('New element added!');
    });
</script>

So when the element is clicked upon it'll write to the console - but can run this function as soon as it's added to the DOM?

I imagine it would look something like this..

<script type="text/javascript">
    /* Just run when it's added... */
    $(document).on(".my-test", function() {
        console.log('New element added!');
    });
</script>

How can I achieve this?

Alex
  • 1,549
  • 2
  • 16
  • 41
  • 1
    I think you are looking for "DOM change events": http://stackoverflow.com/questions/2844565/is-there-a-jquery-dom-change-listener – PzYon Oct 23 '14 at 05:11

0 Answers0