21

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?
Hammad
  • 2,097
  • 3
  • 26
  • 45
  • 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 `
  • `? If so you could just use `.html` which also empties the content and puts the new content.
  • – Selvakumar Arumugam Oct 22 '13 at 20:25
  • 2
    possible duplicate of [Using jQuery to replace one tag with another](http://stackoverflow.com/questions/7093417/using-jquery-to-replace-one-tag-with-another) – zzlalani Oct 22 '13 at 20:25
  • 1
    Do you want to replace the whole UL, or just fill in the contents? – Barmar Oct 22 '13 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` – Trevor Oct 22 '13 at 20:27