harsimranb
harsimranb

Reputation: 2283

Get count of items jQuery.each()

I am parsing XML using jQuery. I want to get the count of all the sub nodes with the given tag name.

For example:

<People>
<person name="hello'></person>
<person name="hello'></person>
<person name="hello'></person>
<person name="hello'></person>
<person name="hello'></person>
</people>

I use the following jQuery Code:

$(xml).find("person").each(function(){});

Of course the above code works, but I just want to get the count, I do not want to loop. The reason is this: The above sample is too easy, my XML file and javascript code is a bit complex, so there is a lot of logic to figure out the xml file, and I don't want to spend code writing all that.

Many Thanks!

Upvotes: 6

Views: 19315

Answers (2)

ZloyPotroh
ZloyPotroh

Reputation: 377

Or also try size():

$(xml).find("person").size();

Upvotes: 1

JaredPar
JaredPar

Reputation: 754853

If you want to get the count then use the length property:

$(xml).find("person").length;

Upvotes: 8

Related Questions