I get 0. However, I have three <li> in my document.
When I run
var listItems=document.getElementsByTagName(“*”) ;
I get a length of 5 in my alert (<html>, <head>, <title>, <meta>, <script>, ). I tested for these tags individually (e.g. getElementsByTagName(“html”)
and they are accounted for.
Why am I unable to target the body, ul, or list tags? I checked my document and it’s essentially the same as the book’s, except for the text between the elements.
What is it that I am missing?
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
<html xmlns="http://www.w3.org/1999/xhtml" lang="en-US">
content="text/html; charset=utf-8" />
Sidney Crosby traded to Vancouver Canucks.
Ovechkin to sign long-term deal with Vancouver Canucks.
Canucks blow out Ducks 8-0.
var listItems = document.getElementsByTagName("li");
var numItems = listItems.length;
post the code exactly as it appears in canucks.js
you need to call the js to get the li elements after the document has finished loading and not before, or at least after the <li>'s have finished loading.
That is the only thing I have in canucks.js right now. The rest I put into comment tags since I was not sure if it was interfering with it.
How do I ensure that the li elements load first before being called?
just above the </body>
leave the call to canucks.js where it is in the <head> and change your code to
Thanks! I spent over an hour last night trying to figure out why it wasn’t working. This is sweet relief.
Another solution instead of using Core.start is to move your script to the end of the body, just before the </body> tag. That way the rest of the body is available by the time the script does its thing.
However, for the sake of working in with the rest of the book, you should stick with using Core.start instead of other solutions.
errata can help to clear up some mistakes that made it in to the book.