HTML Academy
Be careful, children!
Introduction to JavaScript in the browser11/23
Back to the list of tasks
  • 1. Script to start!
  • 2. Ready or not, here I come
  • 3. First class
  • 4. Find every single one
  • 5. Under cover
  • 6. Temporarily unavailable
  • 7. Special offer
  • 8. Summary of “JavaScript in the browser”. Part 1
  • 9. Twelfth program: “Ice cream test”
  • 10. Be careful, children!
  • 11. Creating a card
  • 12. Describe yourself
  • 13. Don’t repeat yourself
  • 14. Refactor and conquer
  • 15. Adding a picture
  • 16. One more function
  • 17. Check yourself
  • 18. Live data
  • 19. Got it in stock? What if I find one?
  • 20. Special offer
  • 21. Start the conveyor
  • 22. Summary of “JavaScript in the browser”. Part 2
  • 23. Thirteenth program: “Ice cream returns”
Describe yourself
  • Sign up
  • Log in

Loading…
Everything will be ready in few seconds

  • Theory
  • Theory
  • Comments

Creating a card

Let’s start adding a product card. If we were to add a new element using markup, then we would add one more <li> with class product to the product list. In JavaScript, you need to take several steps to perform the same task. Let’s take it step by step.

Creating a DOM element

First, you need to create a new list element, li. We can create elements in different ways, we’ll use the createElement() method of document object. The method accepts a string with the name of the tag and returns the created DOM element. This element can be written into a variable for further manipulation:

var card = document.createElement('li');

Note that createElement is the method for the document object. That is, we use it to create an element for this document, not yet specifying where in the DOM it will be located.

Adding a class

You are already familiar with this step. Let’s work with classList of the created element:

var card = document.createElement('li');
card.classList.add('card');

Adding to the DOM-tree

The newly created element by default is not in the DOM tree and is not displayed on the page. We just store it somewhere in a variable. For a new element to appear on the page, you need to add it to the DOM. To do this, let’s find the element (parent) in the current DOM tree and insert our element into it.

We will use the appendChild() method of the parent. This method accepts the element and inserts it into the end of the parent element. That is, if there are already three elements in the list, as in our case, the element added with appendChild will become the fourth one in the list.

var list = document.querySelector('.cards');
var card = document.createElement('li');
card.classList.add('card');

// After calling this method, the new element is rendered on page
list.appendChild(card);

Here’s what happens with the markup after calling appendChild:

<!-- Initial markup state -->
<ul class="cards">
  <li class="card">Existing element</li>
</ul>

<!-- State after calling appendChild -->
<ul class="cards">
  <li class="card">Existing element</li>
  <li class="card">Added element</li>
</ul>

The sequence of adding an element to the DOM can be different: you can create an element using createElement, immediately insert it into the parent element, and then add classes. But this is not the best way: every change in the DOM redraws the page, and this is an expensive operation and it takes a long time.

Now create a product card, add a suitable class to it and put it at the end of the product list. Then log children in the console list to make sure that our card is in the collection of child elements. This is the standard behavior of the collection, it changes on the fly along with changing of the DOM.

Comments

  • index.html
  • style.css
  • script.js
HTML
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>Catalog | Device</title> <link rel="stylesheet" href="device/setting.css"> <link rel="stylesheet" href="style.css"> </head> <body> <ul class="products"> <li class="product product--available"> <h2 class="product__title">Selfie stick for beginners</h2> <img class="product__image" src="device/item-1.jpg" alt="Selfie stick for beginners"> <p class="product__price">200 ₽</p> </li> <li class="product product--unavailable"> <h2 class="product__title">Professional selfie stick</h2> <img class="product__image" src="device/item-2.jpg" alt="Professional selfie stick"> <p class="product__price">1,500 ₽</p> </li> <li class="product product--available"> <h2 class="product__title">Unsinkable selfie stick</h2> <img class="product__image" src="device/item-3.jpg" alt="Unsinkable selfie stick"> <p class="product__price">2,500 ₽</p> </li> </ul> <script src="script.js"></script> </body> </html>
CSS
.products { display: flex; flex-wrap: wrap; justify-content: space-between; width: 570px; margin: 20px auto; padding: 0; list-style: none; } .product { position: relative; display: flex; flex-wrap: wrap; justify-content: space-between; width: 270px; margin-bottom: 30px; } .product:empty { background-color: rgb(225, 225, 239); } .product__image { order: -1; width: 270px; height: 380px; margin-bottom: 15px; } .product__title { max-width: 180px; margin: 0; font-weight: 500; font-size: 18px; line-height: 24px; } .product__price { max-width: 80px; margin: 0; text-align: right; font-weight: 300; font-size: 16px; line-height: 27px; } .product--special { order: -1; width: 100%; height: 380px; flex-direction: column; justify-content: flex-start; align-content: space-between; } .product--special .product__title { width: 270px; max-width: 100%; margin-bottom: 10px; font-size: 28px; line-height: 36px; } .product--special .product__price { width: 270px; max-width: 100%; text-align: left; text-decoration: line-through; color: #cccccc; } .product--special .product__special-price { margin: 0; } .product--special .product__image { outline: 3px solid #ffc600; } .product--special::before { content: "Product of the day"; position: absolute; top: 0; left: 0; display: flex; justify-content: center; align-items: center; padding: 5px 10px; font-weight: 500; color: #111111; text-transform: uppercase; background-color: #ffc600; } .product--available::after, .product--unavailable::after { position: absolute; top: 343px; left: 50%; padding: 10px 5px 2px 5px; border-bottom: 1px solid #57c74b; transform: translateX(-50%); } .product--available::after { content: "In stock"; } .product--unavailable { filter: grayscale(1) opacity(0.7); } .product--unavailable::after { content: "Out of stock"; } .product--special.product--available::after, .product--special.product--unavailable::after { left: 135px; }
JavaScript
var cardList = document.querySelector('.products'); console.log(cardList.children);

What didn’t you like in this task?

Thanks! We’ll fix everything at once!

Click inside the mini browser to put the focus in this window.

100%
Console
Goalscompleted
0
    1. Use createElement to create an element of the li list and write it into the listItem variable.
    2. Add the product class to the list element.
    3. Add listItem to the end of the product list using appendChild.
    4. Below, log the collection of cardList’s child elements again.

    Cookies ∙ Privacy ∙ License Agreement ∙ About ∙ Contacts ∙ © HTML Academy OÜ, 2019−2025

    VISAMastercard

    Log in

    or

    Forgot your password?

    Sign up

    Sign up

    or
    Log in

    Restore access

    Have you forgotten your password or lost access to your profile? Enter your email connected to your profile and we will send you a link to restore access.

    Forgot to connect your email to the profile? Email us and we’ll help.