Get the number of jQuery.each () elements

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

For instance:

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

I am using the following jQuery code:

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

Of course, the above code works, but I just want to get a counter, I don't want it to work. The reason is that the above example is too simple, my XML file and javascript code are a bit complicated, so there is a lot of logic to define an XML file, and I don't want to waste code writing all of this.

Many thanks!

+5
source share
2 answers

If you want to get a counter, use the property length:

$(xml).find("person").length;
+8
source

size():

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

All Articles