Home > other >  getting the length of an array of elements when the number can change (adding or removing elements)
getting the length of an array of elements when the number can change (adding or removing elements)

Time:05-21

So I don't have code for the question but Im curious to know if i wanted to create a ul of li's and want to add and remove the li's, I know the number of li's will be dynamic. So how do I get the number of li's (array indexes) when its adding or removing. I have tried doing const len = document.querySelectorAll("li") and this will only return the length of the array static, if I add or remove li's it wont change the length of the array

CodePudding user response:

$("#id li").length 

will give you the count.

CodePudding user response:

You could try getting the parent element and checking the childElementCount property:

const len = document.querySelector("ul").childElementCount;
  • Related