Trying to convert some jQuery to javascript. Was using this for jQuery:
var list = $('.list li'),
listLength = list.length;
Now I'm trying it with JS, but it doesn't seem to work:
var list = document.querySelector(".list"),
listLi = list.getElementsByTagName("li"),
listLength = listLi.length;
Any ideas?
Trying to convert some jQuery to javascript. Was using this for jQuery:
var list = $('.list li'),
listLength = list.length;
Now I'm trying it with JS, but it doesn't seem to work:
var list = document.querySelector(".list"),
listLi = list.getElementsByTagName("li"),
listLength = listLi.length;
Any ideas?
Share Improve this question asked Jan 8, 2013 at 7:16 user1929705user1929705 211 silver badge8 bronze badges 3- 2 Works just fine for me - jsfiddle/gWvCQ – Andreas Commented Jan 8, 2013 at 7:20
- 1 Your Vannila JS code works just fine. – Sandeep Commented Jan 8, 2013 at 7:22
- 1 Yeah, thanks, just had a typo in the actual code. Oops! – user1929705 Commented Jan 8, 2013 at 7:25
1 Answer
Reset to default 3Your code should already work. But you can also use document.querySelectorAll()
to perform a selector match which is cleaner than the two separate calls:
var listLi = document.querySelectorAll(".list li"),
listLength = listLi.length;