-1

In my app, I create many dynamically objects. some of them needs to be initialized AFTER they are added to the DOM.
I want them to be "smart" and execute some code only when they are added to the dom.

o/c - i can execute the code manually, after insertion. but i am trying to avoid it.

can it be done?
how?

yossi
  • 3,090
  • 7
  • 45
  • 65
  • 1
    Why are you trying to avoid the solution you mentioned? – Rax Weber Jul 03 '17 at 06:42
  • Can you give a specific example - i.e. some code? – kabanus Jul 03 '17 at 06:42
  • Please provide a [mcve] – zer00ne Jul 03 '17 at 06:44
  • @kabanus ha?!?! some code for what? for something that i don't know if its possible at all? – yossi Jul 03 '17 at 07:12
  • @RaxWeber since the objects are being added in multiple locations, lots of them, and it seems like wrong practice. – yossi Jul 03 '17 at 07:13
  • @zer00ne how can i do it, if i can't tell if its' possible?!?! – yossi Jul 03 '17 at 07:14
  • @Akhil not a duplicate, tho it seems that the answer lays there. thanks – yossi Jul 03 '17 at 07:16
  • @yossi SO community overall purpose is to **fix** code that is broken. It is the OP's (Original Poster)'s responsibility to research and prepare a question within the these [guidelines](https://stackoverflow.com/help/asking). If you can't provide any code at all, it's probably because you don't have an app at all. – zer00ne Jul 03 '17 at 07:46
  • @zer00ne https://stackoverflow.com/help/on-topic What topics can I ask about here? Stack Overflow is for professional and enthusiast programmers, people who write code because they love it. We feel the best Stack Overflow questions have a bit of source code in them, but if your question generally covers… a specific programming problem, or a software algorithm, or software tools commonly used by programmers; and is a practical, answerable problem that is unique to software development … then you’re in the right place to ask your question! – yossi Jul 03 '17 at 07:52
  • @yossi Some questions are still off-topic, even if they fit into one of the categories listed above: Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers. See: How to create a [mcve]. – zer00ne Jul 03 '17 at 08:33
  • You should only ask practical, answerable questions based on actual problems that you face. Chatty, open-ended questions diminish the usefulness of our site and push other questions off the front page. Your questions should be reasonably scoped. If you can imagine an entire book that answers your question, you’re asking too much. – zer00ne Jul 03 '17 at 08:35
  • "You should only ask practical, answerable questions based on actual problems that you face" well, asking if i can detect when an object is added to the dom, is exactly that. it seems like we understand things differently.. – yossi Jul 04 '17 at 07:46

1 Answers1

1

You can add your prototype wrap for method .appendChild or use MutationEvents

Node.prototype.myAppend = function (el) {
this.appendChild(el);
el.onAppend && el.onAppend.call && el.onAppend();
}

var div = document.createElement('div');
// handler after append in DOM
div.onAppend = function () {
    alert('Node added in DOM');
}

document.body.myAppend(div);