Skip to content

Instantly share code, notes, and snippets.

@karlbeecken
Last active May 22, 2019 11:45
Show Gist options
  • Select an option

  • Save karlbeecken/4e2d25cb59911828c5bf479fd4a467da to your computer and use it in GitHub Desktop.

Select an option

Save karlbeecken/4e2d25cb59911828c5bf479fd4a467da to your computer and use it in GitHub Desktop.
Create a HTML UL from a javascript array
function makeUL(array) {
// Create the list element:
const list = document.createElement('ul');
for (var i = 0; i < array.length; i++) {
// Create the list item:
const item = document.createElement('li');
// Set its contents:
item.appendChild(document.createTextNode(array[i]));
// Add it to the list:
list.appendChild(item);
}
// Finally, return the constructed list:
return list;
}
// insert the list
document.getElementById('parentElement').appendChild(makeUL(items));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment