Search code examples
jqueryeventsdomcustom-event

how to trigger an event (custom or not) when a generated jQuery element is attached to the DOM


Id there an event that can get triggered when a jQuery dynamically created element is added to any part of the DOM. This would be useful to perform with and height calculation on elements only when any portion of the code in a script is attaching the element to the page or any other page children.


Solution

  • Sorry about my previous answer, I didn't pay enough attention to what you were asking.

    What you're really after is DOM Mutation Events, which has been deprecated, and would be replaced in the future by DOM Level 4 Mutation Observers, which looks promising but isn't implemented in current browsers. Here's a great post about the history and future of this topic.

    Meanwhile, if you don't have any actual access to what's inserting the new nodes (e.g. a 3rd party plugin which doesn't fire the needed events), your best bet would sadly be polling a container for its children().length.