JQuery - Find a dynamically created element without events

This question has been asked before, and the answer is:

$('#container').on('click','#dynamicElement', function(){ /* the code */ });

The above code will be found #dynamicElementwhen you click on it. But what if there is no click or any other event?

Assume the following scenario:

$.ajax(
    url:'file.php',
    data: {'param':'value'},
    success: function(response){
         /*
         how would I get #dynamicElement if it was not click on?
         the element had no event fired at all, nor had any of its parennt
         containers.

          Now what?
         */
    }
);
+4
source share
2 answers

If your new item is added to the page inside the callback success, at this point you can call$('#dynamicElement')

Using $('#dynamicElement')anywhere outside the callback will not return the item, as it has not yet been added to the DOM.

+3
source

, DOM,

$('#dynamicElement)

. , , , , , - , .

, , DOM, , .

, , .

0

Source: https://habr.com/ru/post/1611120/


All Articles