Jquery count xml nodes

I am using jQuery ajax function to handle tracking data from a database. The returned data in this function is similar to

<root> <result city='LA' state='CA'></result> <result city='DALLAS' state='TX'></result> ... </root> 

I use

  var count=$(data).find("result").length(); 

to get the number of result nodes, but this is not true. So how to count result nodes using jQuery?

+7
source share
2 answers

Assuming data is an XML Node object, $(data).find("result").length is fine. Without parentheses, length not a method. There is a method that does the same thing, size() , although there is no real advantage to using it.

(If data was actually a string, you should first parse this into an XML document. Passing non-HTML markup to $() is the wrong thing and sometimes works just luck.)

+6
source

The true length does not work here, I had the same problem, and I decided to use it with size() exactly the same as

 var count=$(data).find("result").size(); 
+1
source

All Articles