Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Manipulating the "data" from $.ajax success: function(data) {

Tags:

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.

like image 892
Hans Avatar asked Dec 10 '09 01:12

Hans


People also ask

What is success function data in AJAX?

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.

How do I return data after AJAX call success?

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); });

What is success and error function in AJAX?

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.

How do I know if AJAX request is successful?

$. ajax({ url: "page. php", data: stuff, success: function(response){ console. log("success"); } });


2 Answers

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:

  • If your data is large, jQuerifying it in-place is slow in IE (at least with jQuery 1.2.6). In that case you may want to create a container for it like var div = jQuery("<div/>"); div.html(data); then manipulate it there before doing $("#id").html(div.html()).
  • If your data has invalid HTML (unclosed tags, etc) you may get weird failures in IE. Just make sure the html is well-formed.
like image 128
jpsimons Avatar answered Oct 14 '22 21:10

jpsimons


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

like image 22
Dave Ward Avatar answered Oct 14 '22 20:10

Dave Ward