HTML Academy
Script to start!
Introduction to JavaScript in the browser2/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”
First class
  • Sign up
  • Log in

Loading…
Everything will be ready in few seconds

  • Theory
  • Theory
  • Comments

Ready or not, here I come

Muffin finally dropped by the office and came to see you first. The boss wants to make a few corrections to the page: he wants to let everyone know that screwdriver “Philly” is the product of the day and mark “Mjöllnir” as not being available for order at this time.

It would seem that JavaScript can do nothing here, since it is possible to just change layout or styles. But we do not have access to either of those. Such was the decision of the store owner. We can only change the page using scripts.

JavaScript takes a special approach to layout: elements here are not strings that we write in HTML files, but objects. In this case, each object is associated with other similar objects and knows about its parent, neighboring object elements and nested objects. The result is a tree structure called DOM (Document Object Model).

Each DOM tree has a root object, from which other objects grow. It’s called document. This global object is available in all programs that run in the browser. Simply put, document is a page that contains all the layout elements (objects).

Developers can use document to find any object on the page and then change it. That is why the document object has special search methods.

The most flexible of these is querySelector. This method takes an CSS selector and returns the appropriate element. It understands any CSS selectors and is called like this:

// Searching for an element by tag
var list = document.querySelector('ul');

// Searching for the last element from the list
var lastProduct = document.querySelector('li:last-child');

// Searching for an element by class name
var price = document.querySelector('.price');

// Searching for the third element from the product list
var thirdProduct = document.querySelector('.product:nth-child(3)');

Let’s look at the store page. We can see a list where each product is <li> with class product. Screwdriver “Philly” must become the product of the day, the second one in the layout. This means that we need to find the second element in the list. To do this, you can use selector .product:nth-child(2). “Mjöllnir” is out of stock and is the last one position-wise. It can be found with .product:last-child.

Let’s find the second and last DOM element from the list of products, log them in the console and make sure that the search worked correctly.

Comments

  • index.html
  • style.css
  • script.js
HTML
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>Catalog | Technomart</title> <link rel="stylesheet" href="technomart/setting.css"> <link rel="stylesheet" href="style.css"> </head> <body> <ul class="products"> <li class="product"> <h2 class="product__title">Combination pliers “Superhook”</h2> <img class="product__image" src="technomart/good-1.jpg" width="200" height="150" alt="Combination pliers “Superhook”"> <p class="product__price">800 ₽</p> </li> <li class="product"> <h2 class="product__title">Screwdriver “Philly”</h2> <img class="product__image" src="technomart/good-2.jpg" width="200" height="150" alt="Screwdriver “Philly”"> <p class="product__price">550 ₽</p> </li> <li class="product"> <h2 class="product__title">Pliers “Copernicus”</h2> <img class="product__image" src="technomart/good-3.jpg" width="200" height="150" alt="Pliers “Copernicus”"> <p class="product__price">1,350 R.</p> </li> <li class="product"> <h2 class="product__title">Puncher “Hello, neighbor!”</h2> <img class="product__image" src="technomart/good-4.jpg" width="200" height="150" alt="Puncher “Hello, neighbor!”"> <p class="product__price">7,500 ₽</p> </li> <li class="product"> <h2 class="product__title">Hammer “Mjöllnir”</h2> <img class="product__image" src="technomart/good-5.jpg" width="200" height="150" alt="Hammer “Mjöllnir”"> <p class="product__price">2,000 ₽</p> </li> </ul> <script src="script.js"></script> </body> </html>
CSS
.product { position: relative; display: flex; flex-direction: column; align-items: center; width: 220px; margin-bottom: 20px; text-align: center; border: 1px solid #cccccc; } .product--available::before { content: ""; position: absolute; top: 5px; left: 5px; width: 15px; height: 15px; padding: 5px; background-image: url("technomart/check.svg"); background-repeat: no-repeat; background-position: center; background-size: 18px; border: 1px solid #4eb543; border-radius: 50%; } .product--unavailable { filter: grayscale(1) opacity(0.7); } .product--unavailable::before { content: "Out of stock"; position: absolute; top: 0; left: 0; padding: 5px; font-size: 14px; } .product--special { flex-basis: 100%; order: -1; border-color: #ee3643; } .product--special::after { content: "Product of the day"; position: absolute; top: 0; right: 0; height: 30px; padding-right: 10px; padding-left: 10px; line-height: 30px; color: #ffffff; background-color: #ee3643; }
JavaScript

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. Declare a variable specialProduct and put there the second DOM element from the product list.
    2. Log specialProduct in the console.
    3. Below, create a variable unavailableProduct and put there the last DOM element from the product list.
    4. Log unavailableProduct variable in the console.

    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.