Last active
May 22, 2019 11:45
-
-
Save karlbeecken/4e2d25cb59911828c5bf479fd4a467da to your computer and use it in GitHub Desktop.
Create a HTML UL from a javascript array
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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