Derek 朕會功夫
Derek 朕會功夫

Reputation: 94309

Get independent HTML from $.ajax()

Assume that in

$.ajax({
    url: 'page1.html',
    data: {},
    success: function (data) {
        $(data)  //return an array of nodes
    },
    dataType: 'html'
});

$(data) returns an array of nodes:

[<div id="a">,
 <div id="b">,  //3 elements
 <div id="c">]

and I want to get the HTML of the div that has an id of "b". I tried

$(data).find("#b");

but it returns a blank array. ([]) So how can I select it? Please help.

Upvotes: 0

Views: 46

Answers (2)

meder omuraliev
meder omuraliev

Reputation: 186562

$(data).filter('#b')

you should use filter instead of find, since there's no parent element for all of those elements so no higher level context to find from.

Upvotes: 1

user1106925
user1106925

Reputation:

  • Use .filter() to select from top level elements.

  • Use .find() to selected nested elements.

Upvotes: 2

Related Questions