/**
* Generate HTML List From JavaScript Array
*
* @source https://getbutterfly.com/generate-html-list-from-javascript-array/
*/
function makeList() {
// Establish the array which acts as a data source for the list
let listData = [
'Blue',
'Red',
'White',
'Green',
'Black',
'Orange'
];
// Make a container element for the list
let listContainer = document.createElement('div');
// Make the list
let listElement = document.createElement('ul');
// Make the list item
let listItem = document.createElement('li');
// Add it to the page
document.body.appendChild(listContainer);
listContainer.appendChild(listElement);
// Set up a loop that goes through the items in listItems one at a time
let numberOfListItems = listData.length;
for (let i = 0; i < numberOfListItems; ++i) {
// Add the item text
// Use this if the array elements contain HTML
// listItem.innerHTML = listData[i];
// If not, use the line below
// Use this if the array elements are text only
listItem.textContent = listData[i];
// Add listItem to the listElement
listElement.appendChild(listItem);
// Reset the list item
listItem = document.createElement('li');
}
}
// Usage
makeList();
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Fiddle Pages
Your fiddles accessible under a custom domain and a vanity path - the simplest hosting for your personal project.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Get Fiddle Pages with PRO
Your fiddles accessible under a custom domain and a vanity path - the simplest hosting for your personal project.