How to select an element loaded by jQuery load () function?

I am currently having problems with the following (here is a sample code):

<div id="container"></div> <script type="text/javascript"> $('#container').load('content.html'); $('.elementInContentHTML').fadeIn(); </script> 

In short, I want to have access to elements that have been dynamically added to the page without binding them to event handlers.

I know about the live() method, but I don’t want to bind my action to any event, that is, I just want to perform some actions with these new elements without pressing them, blur, etc.

+8
javascript jquery ajax asynchronous
source share
2 answers

The load function is asynchronous.
The next line is run before the content is loaded.

You need to put your code inside the load function callback so that it only starts after loading new content:

 $('#container').load('content.html', function() { $('.elementInContentHTML').fadeIn(); }); 
+15
source share

Can you try using a callback to complete the download? See http://api.jquery.com/load/

 $('#result').load('ajax/test.html', function() { alert('Load was performed.'); }); 
+3
source share

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


All Articles