Eric
Eric

Reputation: 2128

Storing JQuery Xml Tree In Variable

Xml:

<Data>
   <Cat>
      <Name>Fluffy</Name>
   </Cat>

   <Cat>
      <Name>Willy</Name>
   </Cat>
</Data>

JQuery:

// ...Some ajax calls...
$(xml).find('Cat').each(function() {
   var name = $(this).find('Name').text();
   alert(name);
});

How can I store the results of find('Cat') in a variable, so that I can pass it to a function and handle it there? I thought something like this, but it doesn't work:

var cats = $(xml).find('Cat');
ShowCatNames(cats);

...

function ShowCatNames(cats) {
   $(cats).each(function() {
       var name = $(this).find('Name').text();
       alert(name);
   }
}

Thank you.

Upvotes: 0

Views: 145

Answers (1)

Musa
Musa

Reputation: 97672

Try parsing the xml first

var cats = $($.parseXML(xml)).find('Cat');

Upvotes: 1

Related Questions