Javascript count list items


/ Published in: JavaScript
Save to your folder(s)



Copy this code and paste it in your HTML
  1. <script type="text/javascript">
  2.  
  3. function count(){
  4. var ul = document.getElementById('ulid');
  5. var i=0, c =0;
  6. while(ul.getElementsByTagName('li')[i++]) c++;
  7. alert(c);
  8. }
  9.  
  10. </script>
  11.  
  12.  
  13. <input type="button" onclick="count()";>
  14. <ul id="ulid">
  15. <li>list 1</li>
  16. <li>list 2</li>
  17. <li>list 3</li>
  18. <li>list 4</li>
  19. <li>list 5</li>
  20. </ul>

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.