Array to HTML List

JavaScriptJavaScriptBeginner
Practice Now

This tutorial is from open-source community. Access the source code

Introduction

In this lab, we will explore how to convert an array into an HTML list using JavaScript. The purpose of this lab is to help you understand how to use Array.prototype.map() and Document.querySelector() to create a list of HTML tags and append them to an existing list in your web page. By the end of this lab, you will have a better understanding of how to manipulate the DOM using JavaScript and create dynamic HTML content.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL javascript(("`JavaScript`")) -.-> javascript/BasicConceptsGroup(["`Basic Concepts`"]) javascript(("`JavaScript`")) -.-> javascript/AdvancedConceptsGroup(["`Advanced Concepts`"]) javascript(("`JavaScript`")) -.-> javascript/DOMManipulationGroup(["`DOM Manipulation`"]) javascript(("`JavaScript`")) -.-> javascript/ToolsandEnvironmentGroup(["`Tools and Environment`"]) javascript(("`JavaScript`")) -.-> javascript/SecurityGroup(["`Security`"]) javascript/BasicConceptsGroup -.-> javascript/variables("`Variables`") javascript/BasicConceptsGroup -.-> javascript/data_types("`Data Types`") javascript/BasicConceptsGroup -.-> javascript/arith_ops("`Arithmetic Operators`") javascript/BasicConceptsGroup -.-> javascript/comp_ops("`Comparison Operators`") javascript/AdvancedConceptsGroup -.-> javascript/higher_funcs("`Higher-Order Functions`") javascript/AdvancedConceptsGroup -.-> javascript/template_lit("`Template Literals`") javascript/DOMManipulationGroup -.-> javascript/dom_select("`DOM Selection`") javascript/DOMManipulationGroup -.-> javascript/dom_manip("`DOM Manipulation`") javascript/ToolsandEnvironmentGroup -.-> javascript/bom("`Browser Object Model`") javascript/SecurityGroup -.-> javascript/xss("`Cross-Site Scripting`") subgraph Lab Skills javascript/variables -.-> lab-28158{{"`Array to HTML List`"}} javascript/data_types -.-> lab-28158{{"`Array to HTML List`"}} javascript/arith_ops -.-> lab-28158{{"`Array to HTML List`"}} javascript/comp_ops -.-> lab-28158{{"`Array to HTML List`"}} javascript/higher_funcs -.-> lab-28158{{"`Array to HTML List`"}} javascript/template_lit -.-> lab-28158{{"`Array to HTML List`"}} javascript/dom_select -.-> lab-28158{{"`Array to HTML List`"}} javascript/dom_manip -.-> lab-28158{{"`Array to HTML List`"}} javascript/bom -.-> lab-28158{{"`Array to HTML List`"}} javascript/xss -.-> lab-28158{{"`Array to HTML List`"}} end

Converting Array to HTML List

To begin coding, launch the Terminal/SSH and enter node.

This function converts the given array elements into <li> tags and adds them to the list with the given id.

Use Array.prototype.map() and Document.querySelector() to generate a list of HTML tags.

const arrayToHTMLList = (arr, listID) =>
  (document.querySelector(`#${listID}`).innerHTML += arr
    .map((item) => `<li>${item}</li>`)
    .join(""));

Example usage:

arrayToHTMLList(["item 1", "item 2"], "myListID");

Summary

Congratulations! You have completed the Array to HTML List lab. You can practice more labs in LabEx to improve your skills.

Other JavaScript Tutorials you may like