EvaldasL
EvaldasL

Reputation: 23

Jquery each html

I have code:

success: function( res ) {                  
    if( res.updated.length > 0 ) {
        $.each( res.updated, function( k,row ) {
            $( '.topStatsOuter' ).html( row.html );
        });
    }
}

When i try alert(row.html), I got all results, but when I use code as above. It adds only one result to div, why?

EDIT.

Tried append. My original div: http://image.prntscr.com/image/3ac6dc5a137e46ababff6acd6bfc2a1a.png

After append: http://image.prntscr.com/image/6b9a2fc093c0440a8d39afd8db0dc274.png I want to overwrite, not add same code

Upvotes: 1

Views: 3888

Answers (2)

Alon Eitan
Alon Eitan

Reputation: 12025

What you're doing at the moment is to overwrite the content of the element ($( '.topStatsOuter' ).html( row.html );).

What you should do is to first empty the content of the element and then append the results using the loop. Your code should be as follows:

success: function( res ) {                  
    if( res.updated.length > 0 ) {
        $( '.topStatsOuter' ).empty();
        $.each( res.updated, function( k,row ) {
            $( '.topStatsOuter' ).append( row.html );
        });
    }
}

Upvotes: 1

Bharatsing Parmar
Bharatsing Parmar

Reputation: 2455

You can try this:

success: function( res ) {                  
    if( res.updated.length > 0 ) {
        var html="";
        $.each( res.updated, function( k,row ) {
            html+=row.html;
        });
        $('.topStatsOuter').html(html);
    }
}

Hope it helps.

Upvotes: 1

Related Questions