Reputation: 4565
I am using the .load()
function in jQuery to load HTML contents stored in a separate file:
$('#div-id').load('contents.html');
And later I am trying to add a class to an element that was loaded from above:
$('#elements_in_contents_html').addClass('active'); // not working
The above has no effect on elements added from contents.html
.
Is it because .load()
does not add the loaded contents to the DOM tree?
Upvotes: 2
Views: 891
Reputation: 722
Use something like this:
$("#myDiv").load( "/somecontroller/someaction", {data: value}, function() {
$("#myDiv").find( "table tbody tr:even" ).addClass( "alt" );
});
This waits until the content has finished loading and then executes your function that attempts to operate on the content.
Upvotes: 0
Reputation: 167220
Try to add it in the callback
, so that it waits for .load()
to get complete and then execute the callback
:
$('#div-id').load('contents.html', function () {
$('#elements_in_contents_html').addClass('active');
});
Upvotes: 2