A friend of mine sent me a massive HTML list of items. This list was not ordered and was not supposed to displayed as ordered. But the total count of items in HTML list was to be displayed on the webpage.
Ordered HTML 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. So, we need to count all the items to show it to the user.
The logic is simple. What we basically do is to get handle of the list you want to count and then walk through the nodes (i.e. list 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
Make sure that you replace ‘my-html-list' with the id of your list. You can have any ID for your list. Also, please note, that you can use this function on ordered lists :-) It works on all both types of lists because both the lists contain LI items.
I hope this piece of code was useful for you. Do let me know if you have any problem with it. Thank you for using TechWelkin.