22

How do I replace the html element from ajax response? What I know to is to remove the element, how do I replace that removed tag with ajax response? For example:

I have code like this:

<ul id="products">
...............
</ul>

When I click on a button the ajax call is made to codeginter controller where I recieve the new data pulled from the database and rendered in another view which starts from ul and ends at closing ul.

In ajax success function I do this:

$('#products').remove();
//What to do now here to replace the removed portion with response of ajax?
4
  • 2
    You could use .append or .html based on what you want. Remember, .remove removes the element altogether, probably you need .empty Show us what was returned from AJAX, is it all <li>? If so you could just use .html which also empties the content and puts the new content. Commented Oct 22, 2013 at 20:25
  • 2
    possible duplicate of Using jQuery to replace one tag with another Commented Oct 22, 2013 at 20:25
  • 1
    Do you want to replace the whole UL, or just fill in the contents? Commented Oct 22, 2013 at 20:26
  • 1
    Where is the rest of your ajax code? Depending on the format of your response.. if your response is in html format then all you need to do is append the response to #products Commented Oct 22, 2013 at 20:27

8 Answers 8

28

You can use replaceWith (see: http://api.jquery.com/replaceWith/)

Like: $('#products').replaceWith(response);

Sign up to request clarification or add additional context in comments.

2 Comments

It is working but the replaced content is ruptured/not well-formed. Let me tell that the new view has slightly changed HTML. Older one shows results in grid format and newer in list format. For replaceWith function the structure of new and old HTML has to be exactly same?
Try $('#products').replaceWith($(response)); maybe. If the styling appears to be different, maybe check your CSS selectors. They need to be the same.
4

Assuming you are replacing your products, if you are getting formatted HTML from your controller then simply do this

success : function(response) {
$('#products').html(response);
}

No need to remove < ul > tag. You can simply replace old < li >s with new < li >s

Comments

4

EDIT: The replaceWith function mentioned by pascalvgemert is better https://stackoverflow.com/a/19527642/2887034

Create a wrapper around it:

<div id="wrapper">
    <ul id="products">
    ...............
    </ul>
</div>

Now you can do the following:

$('#wrapper').html(responseData);

Comments

2

You can use JQuery before

$('#products').before("yourhtmlreceivedfromajax").remove();

Or just replace the content of the div with html $('#products').html("yourhtmlreceivedfromajax");

Comments

2

If your main div is inside another container with another id, you could do so:

Based on this structure:

<div id="mainContainer">
    <ul id="products">
         ...............
    </ul>
</div>

Javascript code using jquery for ajax

$.ajax({
  url: "action.php",
  context: document.body
}).done(function(response) {
  $('#products').remove(); // you can keep this line if you think is necessary
  $('mainContainer').html(response);
});

Comments

2

An easy way is wrap your ul in a container

<div id="container">
<ul id="products">
...............
</ul>
</div>

in ajax response

success:function(data){
$("#container").html(data)
}

Comments

2

.remove() takes the element completely out of the DOM. If you simply want to replace stuff inside the products element, you use .ReplaceWith().

If you are returning all <li> as HTML, you can use .html()

Comments

2

Do this

$( "ul#products" ).replaceWith( response );

http://api.jquery.com/replaceWith/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.