Problem with console
a5rocks (808)
Hmmm so I see two possible meanings to your question:
a) You want all your li
tags to show up: Make your html have <li></li>
b) You want all your tags to show up: Use document.getElementsByTagName('*')
Lvhau_1529 (1)
@a5rocks
I just want it to display like that
First off, why your HTMLCollection in your console is empty is because your
<script>
tag is at the top of your<body>
, so the DOM has not yet loaded when your script is run. Two ways to solve this are:<script>
tag at the bottom of your<body>
tag, so that all the other tags load before your script.window.onload
. This makes it so that the code runs only after everything has loaded.Doing only the first one seems to work in this case, but just to be sure it doesn't break later on, it is usually a good practice to do the second one as well.
Secondly, to show all the tags, either you can use a
for-of
loopOr you can convert the HTMLCollection to an array,
And iterate over it however you want. You can also index the array. For example
tags[1]
will give you the second<li>
element.Hope this helped!