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.

25th Oct 2020, 5:51 PM
Alvedin
Alvedin - avatar
17 Respuestas
+ 3
just use var x = document.querySelectorAll('li).length ; now just print the 'x' varriable
27th Oct 2020, 3:34 PM
Tonmoy Deb
Tonmoy Deb - avatar
+ 2
Try, let n = document.querySelectorAll("li").length Edit: Abhay yeah, I mean to write querySelectorAll, thanks :-)
25th Oct 2020, 6:05 PM
777
777 - avatar
+ 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"){ } }
25th Oct 2020, 6:15 PM
Abhay
Abhay - avatar
+ 1
Can you show what you are trying ?
25th Oct 2020, 6:48 PM
Abhay
Abhay - avatar
+ 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
25th Oct 2020, 7:28 PM
Abhay
Abhay - avatar
+ 1
Thank you all It is solved
27th Oct 2020, 3:35 PM
Alvedin
Alvedin - avatar
0
Burey you know maybe?
25th Oct 2020, 5:58 PM
Alvedin
Alvedin - avatar
0
@vrintle Thank you for the effort, but it doesn't work 🤔
25th Oct 2020, 6:10 PM
Alvedin
Alvedin - avatar
0
Abhay thank you, what is nodeName?
25th Oct 2020, 6:31 PM
Alvedin
Alvedin - avatar
0
Sabit_iz_Tarcina name of the node or html element ,they are in uppercase
25th Oct 2020, 6:35 PM
Abhay
Abhay - avatar
0
No, it doesn't work😏
25th Oct 2020, 6:40 PM
Alvedin
Alvedin - avatar
0
//in js function getNoLists(){ var lists = document.getElementsByTagName("li"); return lists.length } https://code.sololearn.com/WtLA1O6GSx7W/?ref=app
25th Oct 2020, 7:08 PM
Sousou
Sousou - avatar
0
Sabit_iz_Tarcina Can you share the link to your code ? We can find out where you have gone wrong!
27th Oct 2020, 3:14 PM
Sudham Jayanthi
Sudham Jayanthi - avatar
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
27th Oct 2020, 5:13 PM
T1mun
T1mun - avatar
- 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
25th Oct 2020, 6:59 PM
Alvedin
Alvedin - avatar
- 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
25th Oct 2020, 7:54 PM
Alvedin
Alvedin - avatar
- 3
I dont
25th Oct 2020, 9:42 PM
Frankie Roman
Frankie Roman - avatar