Jason Millington
Jason Millington

Reputation: 691

Ajax jquery filter part of the html

I have a Ajax call in Jquery that returns a lot of unwanted HTML. I am trying to filter out that part I want but the console just says empty string? Could someone point me in the right direction please.

$.ajax({
    url: "urlencode.php",
    data: data,
    type: "POST",
    success: function (data) {
        var myd = $(data).filter("#Result").text();
        console.log(myd)
    },

    error: function (request, status, error) {
        alert(request.responseText);
    }
});

Upvotes: 1

Views: 118

Answers (2)

Alessandro Minoccheri
Alessandro Minoccheri

Reputation: 35973

You can pass the html data element to a dynamic div and after find your #Result inside it.
With find() method allows you to search through the descendants of these elements in the DOM tree
Try something like this:

$.ajax({
   url:"urlencode.php",
   data: data,
   type: "POST",
   success: function (data) {
        var myd = $('<div />').html(data).find("#Result").text();
        console.log(myd)
    },

    error: function (request, status, error) {
        alert(request.responseText);
    }
});

Upvotes: 2

Rob W
Rob W

Reputation: 9142

jQuery can do this automatically. In your url parameter, simply use "urlencode.php #Result"

Edit: forgot to mention this works if you use .load.

For your case, try:

success: function (data) {
    var myd = $($.parseHTML(data)).filter("#Result").text();
    console.log(myd)
},

Upvotes: 1

Related Questions