Search code examples
javascriptjqueryfiltergetjquery-data

.filter() return undefined


I have a problem with .filter() for the data returned from $.get().

$.get(url, function(data){
  $(data).filter('[ref=A]').html() // return undefined
});

HTML

<span ref='B'><span ref='A'>abc</span></span>

If I do

 $(data).filter('[ref=B]').html() // return <span ref='A'>abc</span>

After removing <span ref="B"> and do

$(data).filter('[ref=A]').html()// return abc

My question is how can I get the HTML abc with tag <span ref="B">?

Did I use filter() incorrectly? Please advise.


Solution

  • .filter only applies to the outermost element. You want to use .find.

    Additionally, the last span tag is not closed.