0
Get number with Javascript
Hello guys! I have one question regarding number of items inside list in Html. I'll try to explain. In HTML i have <li> some text </li> <li> some text </li> <li> some text </li> So now I want to write function in JavaScript to get number of those list?! In this case that should be number 3. If I had 5 <li> that should show me 5 instead. I don't know if that is possible. I tried with id but didn't get it. I hope you understand my question.
17 Respuestas
+ 3
just use
var x = document.querySelectorAll('li).length ;
now just print the 'x' varriable
+ 2
Try,
let n = document.querySelectorAll("li").length
Edit: Abhay yeah, I mean to write querySelectorAll, thanks :-)
+ 1
@vrintle you mean querySelectorAll not querySelector right?
You can try this as well ,anyway what vrintle did is what I should have thought but I really forgot about it and found some other way if you wanna try
for(i of document.body.children){
if(i.nodeName=="LI"){
}
}
+ 1
Can you show what you are trying ?
+ 1
Sabit_iz_Tarcina I asked for the code that you have written so far,because my solution works fine given you understand what you are doing
+ 1
Thank you all
It is solved
0
Burey you know maybe?
0
@vrintle Thank you for the effort, but it doesn't work 🤔
0
Abhay thank you, what is nodeName?
0
Sabit_iz_Tarcina name of the node or html element ,they are in uppercase
0
No, it doesn't work😏
0
//in js
function getNoLists(){
var lists = document.getElementsByTagName("li");
return lists.length
}
https://code.sololearn.com/WtLA1O6GSx7W/?ref=app
0
Sabit_iz_Tarcina Can you share the link to your code ? We can find out where you have gone wrong!
0
let test = [];
$('li').each(function (i, e) {
test[i] = $(this).text();
});
console.log(test)
for better experience , you can use cheerio
https://cdnjs.cloudflare.com/ajax/libs/cheerio/0.22.0/index.js
- 2
I wrote in my question
I have 3 <li> in body and then in javascript I want to get number of that <li>. In this case it is 3
- 2
Sousou I don't understand why I can't get the result. I saw your code and it works for you but not for me😳
Strange
- 3
I dont