I'd like to trigger an event when an element is created.
$(document).on('load','#TB_title',function() {
console.log('loaded');
});
Is there an equivalent of this that works?
I saw some people suggest livequery, but that seems heavy.
Thanks.
These events are often triggered by the end user's interaction with the page, such as when text is entered into a form element or the mouse pointer is moved. In some cases, such as the page load and unload events, the browser itself will trigger the event.
Example 1: Click event for dynamically added li tags Here myUL- is the id which is already available on DOM when the page loads. jQuery: Now by using . on() will able to bind the click event on dynamically added HTML tag . i.e click event for dynamic added Li tags.
$(this) is a jQuery wrapper around that element that enables usage of jQuery methods. jQuery calls the callback using apply() to bind this . Calling jQuery a second time (which is a mistake) on the result of $(this) returns an new jQuery object based on the same selector as the first one.
I don't think such thing exist directly, but you can handle the DOMSubtreeModified
event and wait until you can find element with such ID:
var _elementToFind = "TB_title";
var _elementFound = false;
var _counter = 1;
$(document).bind("DOMSubtreeModified", function(evt) {
if (_elementFound)
return;
if ($("#" + _elementToFind).length > 0) {
alert("element '" + _elementToFind + "' created");
_elementFound = true;
}
});
Live test case.
The downside is that it's not supported by Opera and IE less than 9 - see here the full details.
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