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?
ajax({ url: 'test. html', dataType: 'html', success: function(response) { $('#testDiv'). html(response); } }); The code above will take all of the code you retrieved with your AJAX query and will output it into the testDiv.
AJAX stands for Asynchronous JavaScript And XML. In a nutshell, it is the use of the XMLHttpRequest object to communicate with servers. It can send and receive information in various formats, including JSON, XML, HTML, and text files.
You can't return "true" until the ajax requests has not finished because it's asynchron as you mentioned. So the function is leaved before the ajax request has finished.
You can use replaceWith (see: http://api.jquery.com/replaceWith/)
Like: $('#products').replaceWith(response);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With