Use jQuery to find out how many list items are in an unordered list.

I am creating a tabbed interface. Content panels are an unordered list created using the server-side script. I want to add tabs via jQuery to control panels. The only requirement for the jQuery plugin to work is to have the same number of panels as the tabs (the number of panels is not always consistent). I need to query the number of list items in my ul.panels and store them in a variable, so I can create a loop with this variable to add list items to ul.tabs.

I do not have access to the server side of the script, so changing PHP is not possible.

+3
source share
2 answers
var count = $("ul.panels li").size();
+4
source

Like this?

 var cnt = $('ul.panels li').length

jQuery is dependent on it, so you don't need your own loop (methinks). You can do this too:

 $('ul.panels li').each( function(idx, panel) {
     // stir in special sauce
 });
+6
source

Source: https://habr.com/ru/post/1713405/


All Articles