When I add something of the class "myClass" I want to call a function on this element.
It would be something in the lines of:
jQuery(".error_message").live("load",function(){
alert("test"+this);
});
... except this doesn't exist.
What's the best way of doing this?
We can detect if an element has been added to DOM using the MutationObserver object. This provides the ability to observe for changes being made to the DOM tree.
jQuery after() Method The after() method inserts specified content after the selected elements.
The jQuery syntax is tailor-made for selecting HTML elements and performing some action on the element(s). Basic syntax is: $(selector).action() A $ sign to define/access jQuery. A (selector) to "query (or find)" HTML elements. A jQuery action() to be performed on the element(s)
Add New HTML Content We will look at four jQuery methods that are used to add new content: append() - Inserts content at the end of the selected elements. prepend() - Inserts content at the beginning of the selected elements. after() - Inserts content after the selected elements.
The short, direct answer to your question would be "no can do." But from your comment a few minutes ago, I see that you want to add elements in different ways and have one unified method of handling these newly added items. May I suggest another approach? Trigger custom events.
The way it would work is like this: everywhere you load the error-message element, you add one line when it's done:
$('.error_message').trigger('load');
Now your .live('load'...) thing will work.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With