JavaScript: Count Items in HTML List

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.

If you are displaying this count of unordered list items on your page –you may not want to update the count every time you update the list. So, a dynamic solution is preferred in such a situation. With the help of JavaScript you can perform this task very easily.

JavaScript is beautiful!

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.

Paste the following JavaScript code in the HEAD section of your page:

<script type='text/javascript'>
function countItems(listID){
var ul = document.getElementById(listID);
var i=0, itemCount =0;
while(ul.getElementsByTagName('li') [i++]) itemCount++;
document.write(itemCount);
}
</script>

And now place the following code at the place where you want to display the count.

<script> countItems('my-html-list')</script>

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.

6 thoughts on “JavaScript: Count Items in HTML List”

  1. Having trouble getting mine to work. It shows it as all code rather than the expected result. I don’t know what I’m doing wrong. I put it in the place I want it to display, and have the first part in the .

  2. Plus Communication

    I have tried your javascript and it works well, but… I would like to count the list on other page than the list. So i have put this code countItems(‘my-html-list’)
    on other page and it doesn`t work. What should i do?

    1. By “other page” if you mean a different HTML page, then JavaScript can not help you. Each URL loads with its private set of JavaScript and JS of one page can not access the HTML elements present on the other pages.

        1. I saw the example that you sighted. JavaScript can’t help in this case. One solution could have been to use a custom PHP page but you’re using WordPress. I think, based on the kind of setup you have, the only solution is manually write the count and update it as and when required.

Leave a Comment

Your email address will not be published. Required fields are marked *