My answer for this is pretty straightforward:
var toppings = ["Tomato", "Cheese", "Pepperoni",
"Olives", "Jalapenos", "Pineapple", "Ham"];
var ul = document.querySelector("ul");
for (var i = 0; i < toppings.length; i++) {
var topping = toppings[i];
var listItem = document.createElement("li");
listItem.textContent = topping;
ul.appendChild(listItem);
}
This assumes you put the script under the element you want to populate. Then uses innerHTML to set the contents.
var scripts = document.getElementsByTagName('script');
var currScript = scripts.item(scripts.length - 1);
currScript.previousElementSibling.innerHTML = `<li>${toppings.join('</li><li>')}</li>`;
Iâve meant to mention this a few times while visiting the forum. I donât ever see the images that should accompany this and similar posts. When I inspect the broken image icon the dev console states that the images are not being loaded because they are mixed content, an http image source served over a https connection. Do others see the images?
A simple change to a âfor ofâ loop iteration.
let listItem = document.querySelector("ul")
for (let value of toppings) {
listValue = document.createElement("li");
listValue.textContent = value;
listItem.appendChild(listValue);
}
No, I donât. Itâs one of those things I assumed was known to be broken and maybe being worked onâŚ?
Posts like this usually have a clickable link to the article as well (this one has the link but it was not converted into an anchor) and is shown in a slightly different format which was unusual as well. I tend to depend on that link to see what the post is linked to.
Edit: I just noticed @kirupa edited the original post which I imagine broke the standard formatting normally seen with these linked posts. (Note: this doesnât pertain to the broken images aspect, just the clickable link I mentioned on top of that.)
jQuery 
$('ul').append(toppings.map(t => $('<li>').text(t)));
That part of the forums has always been broken, and I havenât looked into fixing it. It is something that gets auto-generated when a new article on the site gets posted. Iâll look into why the links arenât working, though my preference is to not have the entirety of the content duplicated in the forums 
I agree. Iâd rather there just be a link.
The broken links are from the path resolution being broken. It comes together as kirupa.comimages/⌠missing a â/â and the subdirectory (ies?) between the domain and images. In some cases its â/react/â, here its â/html5/â etc.
var ul = document.querySelector(âulâ);
Is there any advantage to using this versus the following:
var ul=dcoument.getElementsByTagName(âulâ)[0] ?
Itâs more efficient. In more ways than one:
- Shorter to write
- Only needs to find the first match, not all other matches
- Doesnât require creation of an intermediary array to contain all matches
- Doesnât require the additional
[0]lookup
Youâre also not limited to tag names in querySelector. You can use any CSS selector, such as 'ul.myClass button[disabled]'
Thanks for the explanation. I have not used querySelector much so I guess Iâll have to learn about it and start to use it more.
If you are looking for some additional resources around querySelector, this tutorial and video may help you out: https://www.kirupa.com/html5/finding_elements_dom_using_querySelector.htm

Thanks Kirupa. This is EXACTLY what I was looking for. I got your code working. If you could just explain how I can do the following 2 things I will have everything I need.
- How to identify which listview item was clicked
- Change the colour of the listview item on hover
Thanks
Paul
For your first question, this should help: https://www.kirupa.com/html5/handling_events_for_many_elements.htm Listen for the click event on the ul element and use the target property to figure out which list element triggered the click.
For the listview hover color, you can do something like this:
li:hover {
background-color: #EEE;
}

Hi Kirupa,
I was practicing with your js list sample but, when I put the code on separate js file it wont work. It work fine on the Google Console. Do you know what Im missing?
Can you share your HTML and JS code? You can paste them here, select what you pasted, and click in the </> button in the toolbar to format them as code 