Faizan Ali
Faizan Ali

Reputation: 509

Getting the value of XML element from array of elements in jQuery

I get the array of xml nodes using jquery with the following code

var appointments = new Array();
appointments = $($myXML).find('confirmationNumber');

Which returns me:

[
  <confirmationNumber>​NX0FH25P​</confirmationNumber>​, 
  <confirmationNumber>​VX0MW251</confirmationNumber>​, 
  <confirmationNumber>​VB0TH252​</confirmationNumber>​,
  <confirmationNumber>​VB0MH253</confirmationNumber>​
]

I want to retrieve the value of the following confirmation numbers as texts in array and I dont want to iterate the whole XML

I tried:

appointment[i].text() and appointment[i].val(); 

but that didn't work.

Upvotes: 2

Views: 1638

Answers (1)

Fr&#233;d&#233;ric Hamidi
Fr&#233;d&#233;ric Hamidi

Reputation: 262939

You can use map() to project the text of the elements into a new array without explicitly looping:

var confirmationNumbers = $($myXML).find("confirmationNumber").map(function() {
    return $(this).text();
}).get();

Upvotes: 2

Related Questions