Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery and load(). Unable to change inner HTML in loaded class [duplicate]

I'm trying to change inner HTML in loaded div by this function:

$("#container").load("site.php");

In site.php I have <div class="users_count"> and I would like to put some text inside it. I'm trying to use $(".users_count").html("TEST") but it can't be done since jQuery do not see .users_count class.

I've heared about jQuery on() and live() but it requires some event like click, keyup etc...

I need to put some text in this div without any event - just do it in my function without user action.

like image 372
Filemon279 Avatar asked Dec 09 '16 10:12

Filemon279


2 Answers

You could use the complete callback of load() :

If a "complete" callback is provided, it is executed after post-processing and HTML insertion has been performed.

$("#container").load("site.php", function() {
    $(".users_count").html("TEST")
});
like image 195
Zakaria Acharki Avatar answered Oct 23 '22 05:10

Zakaria Acharki


$.fn.load() is async, you could use complete callback:

$("#container").load("site.php", function(){
  $(this).find('.users_count').html('TEST');
});

$(this).find('.users_count') isntead of just $('.users_count') because it is better to set relevant context, you wouldn't want in most cases target elements not descendant of loaded content, depending selector used. In your case it would change nothing but sometimes who knows...

like image 40
A. Wolff Avatar answered Oct 23 '22 04:10

A. Wolff