Ok so I'm trying to understand the logic of this example from a book because I cant find any documentation to support this syntax and yet it seems to work.
Now the example below is stripped from a larger code segment, but for the purpose of what I'm trying to explain/understand I'm only showing the pertinent parts.
If you need to see the full example then you can download it here:
http://javascriptbook.com/code/c06/
and it's the Using Mutation Events example.
All it's doing is counting all the li elements in this particular ul element and display that number.
The HTML
<ul id="list">
<li>Item 1</li>
<li>Item 2</li>
<li>Item 3</li>
</ul>
<h2><span id="counter">Number of Items</span></h2>
The JavaScript
var listitems = list.getElementsByTagName('li').length;
var counter = document.getElementById('counter');
counter.innerHTML = listitems;
That's the example from the book and it works as it should. I just can't understand it because I have never seen an id name infront of getElementsByTagName method. I know you can use document.getElementsByTagName and element.getElementsByTagName but this appears to be using the id name unless I'm missing something here. "list" is neither document nor an element name. The only reference to it in the code is the id name for the ul element.
The only way I know to do this is by...
var list = document.getElementById('list');
listitems = list.getElementsByTagName('li').length;
var counter = document.getElementById('counter');
counter.innerHTML = listitems;
or if you want it all on one line...
var listitems = document.getElementById('list').getElementsByTagName('li').length;
var counter = document.getElementById('counter');
counter.innerHTML = listitems;
...and both of those work. But again so does the original example above from book and I have no clue why. I can't find anything to support or explain why and how using an id name with the getElementsByTagName method works.
Can anyone assist here with some explanation? Appreciate it.