1

I have an external function which has to be called for all elements with a certain class, like this:

jQuery('.myClass').myFunction();

This works, but how can I make this happen for dynamically created elements. I have multiple functions which can create elements, and I don't want to add this line in every piece of code which creates a new element.

The reason for this is that i load javascript based on what elements i use with php. I therefore cant add myFunction() to the element create function, since both functions are in different files which might or might not be loaded together on the same page.

So how can I call the .myFunction() function on an object with '.myClass', as soon as it is created.

So in the function that creates the element, i don't know what functions need to be called on the element, and in the file that executes the function on the elements, i don't know which functions create an element.

Solution: Since this is marked as a duplicate question, i cannot add an answer. However, i did take a different approach to solve this than in that question. My approach was creating a new class where every script can add functions which should be called when creating an element, and calling a those functions when creating a new element.

Js:

function jsHandler(){
    this.actions = [];

    this.addAction = function(action){
        this.actions.push(action);
        action();
    };

    this.callActions = function(){
        jQuery.each(this.actions,function(key,action){
            action();
        });
    };
}
var myJsHandler = new jsHandler;//global variable

Then in each file which has to call a function over objects, i put this:

myJsHandler.addAction(function(){
    jQuery('.myClass').myFuncion();
});

In every file which creates an element, i put this after creating the element:

myJsHandler.callActions();

For me this works. Some notes though:

  • I probably need to add some code so that jsHandler is always created before the other scripts are loaded.
  • Note that this function calls all functions each time an element is created. For my functions this doesn't matter.
user4493177
  • 580
  • 9
  • 30
  • You can add a line of code `.trigger('myFunction')` and make it part of your dynamic element creation process. – Caner Akdeniz Mar 06 '15 at 15:55
  • 1
    @Connor There is no good accepted answer for that question. The top answered question says: Run that code when adding new buttons, But i dont want to do that since i have multiple functions which can create new elements. – user4493177 Mar 06 '15 at 16:04
  • @CanerAkdeniz Where should i write that? – user4493177 Mar 06 '15 at 16:04
  • @user4493177 Maybe its because the person who asked hasn't logged in for 4 years and plus, i think you're just being lazy either 1. Do it every time 2. put the code in a shorter function, and do it every time. or 3. research how to be notified every time a element is added to the dom, then run it. – iConnor Mar 06 '15 at 16:10
  • @user4493177 you'd want to use method 1 of this answer: http://stackoverflow.com/a/14060809/1907358 – iConnor Mar 06 '15 at 16:11
  • @Connor Even if i was being lazy that does not change the fact that the anwer given in your link does not provide an answer to my question. And also looking for a method to do something in a simpler way does not make this an invalid question. – user4493177 Mar 06 '15 at 16:14
  • @user4493177 -- You'd have to call the function each time the element is added...jQuery doesn't have element watchers. How many different methods can add buttons, and are they related methods? – tymeJV Mar 06 '15 at 16:16
  • @user4493177, if you can provide the block of code where you create the dynamic content, I would be able to help where to place the function call. – Caner Akdeniz Mar 06 '15 at 16:16
  • @user4493177 The link i sent you is the answer to your question. you need to observe the document for any elements being dynamically created and inserted. – iConnor Mar 06 '15 at 16:17
  • @connor The reason i dont want to add id manually is because i have multiple functions like this, and multiple functions which can create elements. That means that in every element-creation function i have to add myFunction(), but also myFunction2 if i get one etc. Also, if i create a new function, i have to add it to all element-creation functions and vica versa. – user4493177 Mar 06 '15 at 16:17
  • @user4493177-- You seem to not want to accept that that may be the easiest way to do this... why? – tymeJV Mar 06 '15 at 16:19
  • @user4493177 I have to go, you should read the link i sent you. go to MDN and research it just because someone hasn't sent your code back to you in working order doesn't mean your question hasn't been answered. **Good Luck!** – iConnor Mar 06 '15 at 16:19
  • @CanerAkdeniz ,@tymeJv I load javascript functions depending on when i need them. This means that the content-functions (myFunction() in this case) and the element-create-functions can change. So that means i would call every function in every element-create function. – user4493177 Mar 06 '15 at 16:22
  • @user4493177 if your function calls depends on some conditions, then you would have to call different functions one by one anyway. So lets say, if you have 2 functions creates different dynamic content, and if the content is assigned to an element like `myHtml`, then right before you append it to the DOM element, you would call `$(myHtml).trigger('myFunction)`, and then your append code like `$('.myElement').append(myHtml)`. – Caner Akdeniz Mar 06 '15 at 16:26
  • @CanerAkdeniz But that does not solve the problem of adding it to each creation-function. – user4493177 Mar 06 '15 at 16:34

1 Answers1

2
<div class="root">
  <span class="line"></span>
</div>


jQuery(document).on('DOMNodeInserted','.line',function(){
     jQuery(this).myFunction();
     console.log("object added");})
jQuery(".root").append(jQuery("<span/>").addClass("line"))

for details and Mutation events list this link

Haydar C.
  • 941
  • 9
  • 17
  • Thank you, i was looking for something like this. However, i also found that domNodeInserted is deprecated and will not be supported much longer. – user4493177 Mar 06 '15 at 16:28
  • 1
    https://github.com/uzairfarooq/arrive/ DOM elements creation and removal!. If you want this look. – Haydar C. Mar 06 '15 at 16:42