Skip to content

Convert an array of elements into a HTML list in JavaScript

This snippet written in JavaScript converts the elements of an array into tags and appends them to the list of the given ID. The <li> tag defines a list item. The <li> tag is used in ordered lists (<ol>), unordered lists (<ul>) and in menu lists (<menu>)

const arr2li = (arr, listID) =>
  (el => (
    (el = document.querySelector('#' + listID)),
    (el.innerHTML += => `<li>${item}</li>`).join(''))
arr2li(['element1', 'element2'], 'your_list_ID');
See also  How to handle paste event in textarea HTML element?

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.