Get the li count of a specific ul in javascript

Updated at 04-Jul-2022, By samar

Get the li count of a specific ul in javascript

In this article, we will see how to solve "Get the li count of a specific ul in javascript".

I want to get the count of all LI (List item) of a specific UL in Javascript. How can i get the count of the all list item by targeting an unordered list of HTML.
  • <ul id="ul_list">
        <li>Lorem ipsum dolor sit amet consectetuer.</li>
        <li>Aenean commodo ligula eget dolor.</li>
        <li>Aenean massa cum sociis natoque penatibus.</li>
    </ul>
    
    <script>
        var listCount = document.getElementById("ul_list").getElementsByTagName("li").length;
    	alert(listCount);
    </script>
    
    It will return the count of all (LI) items of an UL.

Back to code snippet queries related javascript