Azeez
Azeez

Reputation: 495

Append data to existing element data without clearing div

I am working with php ajax where I have to append some long html so I use html function for this job and it was working for me before.But this time when i try to append item using html function its clear all the li inside ul and then append data and if i try to append the data again its does not append li again .I cant figure out what is the main problem but i use the same way before in my code but this time its clear all the li and then append. Here is my code.

//UL where i have to append data
<ul class="review-list">
</ul>

Here is my php ajax response

  echo "<li>
<div class='product-comment'>
    <img src='assets\images\icons\author.png alt=''>
    <div class='product-comment-content'>
        
        <p class='meta'>
            <strong>$review_user_name</strong> - <span>$date</span>
            <div class='description'>
                <p>$review_comment</p>
            </div>
    </div>
</div></li>";

Here is my jquery code from ajax response

success:function(response){
            if(response == 2){
            window.location.href = 'loginregister.php';
             
            }else{
            $('.review-list').html(response);}}

Upvotes: 2

Views: 403

Answers (2)

Hitesh Kumar
Hitesh Kumar

Reputation: 373

Use the append method instead of html method in your else statement

$('.review-list').append(response);

Upvotes: 1

Always Helping
Always Helping

Reputation: 14570

You can simply use jQuery append() function.

By using append() your existing li Will stay and all the other data will be appended afterwards

Change your else To this and it will work fine.

$('.review-list').append(response)

Upvotes: 1

Related Questions