Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jquery count xml nodes

Tags:

jquery

xml

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

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

I'm using

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

to get count of result nodes, but it is not right. So how to count result nodes using jQuery?

like image 915
Steven Zack Avatar asked Oct 25 '11 16:10

Steven Zack


2 Answers

Assuming data is an XML Node object, $(data).find("result").length is fine. No brackets, length is a property not a method. There is a method that does the same thing, size(), though there's no real advantage to using it.

(If data were actually a string, you'd have to parse that into an XML document first. Passing non-HTML markup to $() is the wrong thing and only works sometimes by luck.)

like image 127
bobince Avatar answered Nov 02 '22 23:11

bobince


It's true length is not work here, I had same problem and I had solved it using size() just like

var count=$(data).find("result").size();
like image 28
Prakash Malviya Avatar answered Nov 02 '22 23:11

Prakash Malviya