Skip to content

Latest commit

 

History

History
30 lines (25 loc) · 1.17 KB

arrayToHtmlList.md

File metadata and controls

30 lines (25 loc) · 1.17 KB
title tags author_title author_url author_image_url description image
arrayToHtmlList
browser,array,intermediate
Deepak Vishwakarma
Implementation of "arrayToHtmlList" in typescript, javascript and deno.

TS JS Deno

Converts the given array elements into <li> tags and appends them to the list of the given id.

Use Array.prototype.map(), document.querySelector(), and an anonymous inner closure to create a list of html tags.

const arrayToHtmlList = (arr: (string | number)[], listID: string) => {
  let el = document.querySelector("#" + listID);
  if (el) {
    el.innerHTML += arr.map((item) => `<li>${item}</li>`).join("");
  }
};
arrayToHtmlList(["item 1", "item 2"], "myListID"); // <li>item1</li><li>item2</li>