The other day I needed to dynamically count the total number of items in an HTML unordered list. The ordered list (created with OL tag) are by default counted as each item is preceded by its number in the list. But the lists created using UL tag are not numbered.
The logic is simple. What we basically do is to get the handle of the list you want to count and then loop through the items while incrementing a counter. When the loop is done, counter variable would contain the total number of items. Then you can print this count wherever you want on your web page.
And now place the following code at the place where you want to display the count.
SEE ALSO: Randomize the items in an HTML list
Note 1: Replace ‘my-list’ with the id of your list.
Note 2: It’s not that you can’t use this function on ordered lists! You can.