Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery event when element is created

Tags:

jquery

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.

like image 594
rpophessagr Avatar asked Feb 13 '12 15:02

rpophessagr


People also ask

How is an event generated in jQuery?

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.

How can call Click event dynamically in jQuery?

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.

What is $( this in jQuery?

$(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.


1 Answers

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.

like image 56
Shadow Wizard Hates Omicron Avatar answered Nov 13 '22 00:11

Shadow Wizard Hates Omicron