1

I have a page and I am using ajax to load the data from a database. That data contains some html code and I put this code inside a div called #container. I have a paginator system, so if I click on "next" button, the ajax starts again and gets the html code as response, and then replaces this code with the previous code inside a #container.

I have code something like this:

function loading_show() {
    $('#loading').html("<img src='images/loading.gif'/>").fadeIn('fast');
}
function loading_hide() {
    $('#loading').fadeOut('fast');
}
function loadData(page) {
    loading_show();

    $.ajax({
        type: "POST",
        url: "load_data.php",
        data: "page=" + page,
        success: function(msg) {
            $("#container").ajaxComplete(function(event, request, settings) {
                loading_hide();
                $("#container").html(msg);
            });
        }
    });
}

The result of the ajax function is a HTML message. A part of this message is something like this:

<span id="one_15" class="textzero">8002379154 - JKLM REPRESENTACIONES Y CIA LTDA</span> 

OO. So.. if I make the ajax call again I get another html like this:

<span id="one_16" class="textzero">EXAMPLE TEXT</span> 

The problem is that, say if I want to do something with textzero class after loading before the textzero class preserv the previous ajax data like this:

texto2 = $('.textzero').text();

<span id="one_15" class="textzero">8002379154 - JKLM REPRESENTACIONES Y CIA LTDA</span>
<span id="one_16" class="textzero">EXAMPLE TEXT</span> 

Question: Is there a way to clean the previous elements after load replacement data with an ajax call?

Yang
  • 8,580
  • 8
  • 33
  • 58
JuanFernandoz
  • 805
  • 17
  • 40

1 Answers1

0

You can call $("#container").empty() to remove all child nodes from that container.

Piet van Dongen
  • 1,629
  • 10
  • 13