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?
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.)
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();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With