Steve Phuc
Steve Phuc

Reputation: 1278

Jquery use find and remove in ajax response

I have a code use jQuery, and ajax

$.post(
  'http://example.com', {
    abc: $abc
  },
  function(data) {

    $.post(
      'http://example.com/123', {
        cde: cde
      },
      function(data12) {

        $.each($.parseJSON(data12), function(idx, obj) {

          var $response = $(data);
          $xyz = $response.filter('.abc[value="' + obj + '"]');
          $xyz.remove();
          console.log($xyz.html());
        });
      }
    );
  }
);

Expamle:

data =

<div class="abc" value="1"><span>abc</span></div>
<div class="abc" value="2"><span>def</span></div>
<div class="abc" value="3"><span>ghj</span></div>
<div class="abc" value="4"><span>xyz</span></div>

date12 = ["1","2"]

console = <span>abc</span> <span>def</span> <span>ghj</span> <span>xyz</span>

but the one I want is console = <span>ghj</span> <span>xyz</span>

The things I want is use jquery find and remmove something in ajax resposive and then save it.

Can anyone help me how to do please.

Upvotes: 2

Views: 1650

Answers (1)

Andrew Dunai
Andrew Dunai

Reputation: 3129

I guess you want this: JQuery, remove element from string

Here's the actual code duplicated from that answer:

var s = '<h1>heading</h1><p>para</p>';

var $s = $(s).not('h1');

$('body').append($s);​

...or this one, also from that thread:

var s = '<div><h1>heading</h1><p>para</p></div>';

var $s = $(s).find('h1').remove().end();

$('body').append($s);​

EDITED: If you have more than 1 top-level element within the string, you'll have to wrap it inside another div beause $() function wants string with a single top-level element:

...
var $s = $('<div>' + s + '</div>').find(...).remove()
...

Upvotes: 1

Related Questions