# Dynamically Create a List

**URL:** <https://forum.kirupa.com/t/dynamically-create-a-list/653852>\
**Category:** web dev\
**Created:** [October 4, 2022, 4:10am UTC](https://forum.kirupa.com/t/dynamically-create-a-list/653852 "2022-10-04T04:10:40Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![kirupa](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/kirupa/32/11616_2.png) [@kirupa](https://forum.kirupa.com/u/kirupa)\
**Post date:** [October 4, 2022, 4:10am UTC](https://forum.kirupa.com/t/dynamically-create-a-list/653852/1 "2022-10-04T04:10:40Z")

</div>

This is the companion topic to the **Dynamically Create a List** coding exercise:

> **[Dynamically Create a List : Frontend Coding Exercises](https://www.kirupa.com/html5/dynamically_create_populate_list.htm)**
>
> Show off your DOM skills in this fun exercise where you get to dynamically generate a list.

---

<div class="post-metadata">

**Author:** ![steve.mills](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/steve.mills/32/14961_2.png) [@steve.mills](https://forum.kirupa.com/u/steve.mills)\
**Post date:** [October 4, 2022, 10:06am UTC](https://forum.kirupa.com/t/dynamically-create-a-list/653852/2 "2022-10-04T10:06:04Z")

</div>

```auto
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Coding Exercises Start</title>
  <style>
    body{margin: 0; padding: 2rem}
    h1{color: grey; font-size: 3.5rem;}
    ul{display: contents;}
    li{background-color: yellow; list-style-type: none; font: 1rem arial; font-weight: bold; margin-block: 1.25rem; max-width: fit-content; padding: .25rem;}
  </style>
  <script>
    class pizzaList extends HTMLElement{
      static data = {
        title: "Toppings",
        items: [
          'Tomato', "Cheese", "Pepperoni", "Olives", 
          "Jalepenos", "Pineapple", "Ham"
        ]
      }
      connectedCallback (){
        this.appendChild(Object.assign(document.createElement('h1'),{textContent: pizzaList.data.title}))
        let ul = this.appendChild(document.createElement('ul'))
        for(let i of pizzaList.data.items){ ul.appendChild(document.createElement('li')).textContent = i}
      }
    }
    customElements.define('pizza-list', pizzaList)
  </script>
  </head>
<body>
<pizza-list></pizza-list>
</body>
</html>

```

---

<div class="post-metadata">

**Author:** ![kirupa](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/kirupa/32/11616_2.png) [@kirupa](https://forum.kirupa.com/u/kirupa)\
**Post date:** [October 5, 2022, 12:13am UTC](https://forum.kirupa.com/t/dynamically-create-a-list/653852/3 "2022-10-05T00:13:14Z")

</div>

Another solution would be to use string literals and innerHTML to insert the ul/li elements directly. That fits the constraints where one must use JavaScript to get the elements to appear.

🙂

---

<div class="post-metadata">

**Author:** ![senocular](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/senocular/32/7217_2.png) [@senocular](https://forum.kirupa.com/u/senocular)\
**Post date:** [October 5, 2022, 1:37pm UTC](https://forum.kirupa.com/t/dynamically-create-a-list/653852/4 "2022-10-05T13:37:13Z")

</div>

You think this pizza was made at a _chain_ restaurant? 🥁

```javascript
document
  .body
  .appendChild(
    document
      .createElement("style")
  )
  .appendChild(
    document
      .createTextNode("li{list-style:none;margin:2em 0;}")
  )
  .parentNode
  .appendChild(
    document
      .createTextNode("li span{border-radius:.25em;background:#f9d748;padding:.5em .25em;font-family:sans-serif;}")
  )
  .parentNode
  .parentNode
  .appendChild(
    document
      .createElement("ul")
  )
  .appendChild(
    document
      .createElement("li")
  )
  .appendChild(
    document
      .createElement("span")
  )
  .appendChild(
    document
      .createTextNode("Tomato")
  )
  .parentNode
  .parentNode
  .parentNode
  .appendChild(
    document
      .createElement("li")
  )
  .appendChild(
    document
      .createElement("span")
  )
  .appendChild(
    document
      .createTextNode("Cheese")
  )
  .parentNode
  .parentNode
  .parentNode
  .appendChild(
    document
      .createElement("li")
  )
  .appendChild(
    document
      .createElement("span")
  )
  .appendChild(
    document
      .createTextNode("Pepperoni")
  )
  .parentNode
  .parentNode
  .parentNode
  .appendChild(
    document
      .createElement("li")
  )
  .appendChild(
    document
      .createElement("span")
  )
  .appendChild(
    document
      .createTextNode("Olives")
  )
  .parentNode
  .parentNode
  .parentNode
  .appendChild(
    document
      .createElement("li")
  )
  .appendChild(
    document
      .createElement("span")
  )
  .appendChild(
    document
      .createTextNode("Jalapeños")
  )
  .parentNode
  .parentNode
  .parentNode
  .appendChild(
    document
      .createElement("li")
  )
  .appendChild(
    document
      .createElement("span")
  )
  .appendChild(
    document
      .createTextNode("Pineapple")
  )
  .parentNode
  .parentNode
  .parentNode
  .appendChild(
    document
      .createElement("li")
  )
  .appendChild(
    document
      .createElement("span")
  )
  .appendChild(
    document
      .createTextNode("Ham")
  )

```

---

<div class="post-metadata">

**Author:** ![kirupa](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/kirupa/32/11616_2.png) [@kirupa](https://forum.kirupa.com/u/kirupa)\
**Post date:** [October 5, 2022, 4:25pm UTC](https://forum.kirupa.com/t/dynamically-create-a-list/653852/5 "2022-10-05T16:25:25Z")

</div>

![](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/8/f/8f73a110d906156d4fb1bf89f28942620a69362b.gif)  
You both got badges!

😛
