i have this and a simple question to it.
$.ajax({ type: "POST", url: "/", data: $(".form").serialize(), dataType: "html", success: function (data) { $("#id").html(data); } });
Inside "data" is some html I am inserting into the DOM. Thats no problem. But I want to manipulate the "data" before doing so. How can I do that? For example there are some li elements in "data". How would I for example delete the last li element out of the "data" string before inserting it into the DOM?
I tried something like
$(data li:last)remove();
...but that didnt work.
Thanks for your help.
What is AJAX success? AJAX success is a global event. Global events are triggered on the document to call any handlers who may be listening. The ajaxSuccess event is only called if the request is successful. It is essentially a type function that's called when a request proceeds.
You can store your promise, you can pass it around, you can use it as an argument in function calls and you can return it from functions, but when you finally want to use your data that is returned by the AJAX call, you have to do it like this: promise. success(function (data) { alert(data); });
success and Error : A success callback that gets invoked upon successful completion of an Ajax request. A failure callback that gets invoked in case there is any error while making the request.
$. ajax({ url: "page. php", data: stuff, success: function(response){ console. log("success"); } });
You don't need a hidden DIV. If you want to convert an html string to a DOM fragment, simply call jQuery on it. In your example:
success: function(data) { var jqObj = jQuery(data); jqObj.find("li:last").remove(); $("#id").empty().append(jqObj); }
Some IE considerations though:
var div = jQuery("<div/>"); div.html(data);
then manipulate it there before doing $("#id").html(div.html())
.You can create a jQuery object from arbitrary HTML, e.g.:
$('<ul><li>Item</li></ul>');
So, you could do something like this:
success: function(data) { var $list = $(data); $list.find('li:last').remove(); // Do something with $list here, like append(). }
Here's a working example you can play around with on JS Bin: http://jsbin.com/ejule3/edit
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