Information about cookies on this page

Our website uses cookies to ensure the functionality of the website, to collect and analyze information about the performance and usage of the website. Privacy Policy

  • Courses
    • Discord
    • Twitter
    • Linkedin
    • Facebook
    • Instagram
  • Pricing
  • English
  • Čeština
  • Deutsch
  • Русский
Sign inStart free
  • Courses
  • Pricing

Community

Sign in
  
  • Courses
    • Discord
    • Twitter
    • Linkedin
    • Facebook
    • Instagram
  • Pricing
  • English
  • Čeština
  • Deutsch
  • Русский
Sign inStart free
  • Courses
  • Pricing

Community

Sign in

Summary

In this lesson, you learned that:

  • A page is written in HTML and made of elements, written with tags: <h1>Title</h1>. Elements can have attributes, such as id (a unique name) and class (the name of a group).
  • The page loads a script with <script src="script.js"></script> at the end of body, so that the elements exist when the script runs. The script runs when the page loads; what it prints and its errors appear in the console under the page.
  • document.querySelector() finds the first element that matches a selector: "h1", "#id" or ".class". When nothing matches, it returns null, and using it ends with an error such as TypeError: Cannot read properties of null (reading 'textContent').
  • textContent is the text of an element; you can read it and change it.
  • element.style.color = "red" changes one style (CSS names in camelCase, such as backgroundColor); classList.add(), remove() and toggle() add and remove classes from the page's CSS.
  • button.addEventListener("click", function () { ... }) runs the function on every click. Pass the function itself, without ().
  • The data lives in variables (a counter in a let variable, items in an array); a handler changes them and shows them on the page again.
  • input.value is the text in a field; Number() turns it into a number.
  • document.createElement("li") creates an element and list.append(item) puts it into the page. To show an array, clear the list with list.textContent = "" and create one item for each element in a loop.

In the next lesson, Events and Forms, your pages will react to more than clicks: to typing, to choosing from a list, to pressing keys and to sending forms.

But first, a little practice: a piggy bank that adds up your savings. This time, script.js is empty, and you'll write the whole program yourself.

Instructions

  1. 1

    Make the piggy bank work. In script.js, find the elements you need. When the button #save is clicked, add the number typed into #amount to a variable saved (create it with the value 0), and show the total in #saved as Saved: 150 (after saving 100 and 50).

  2. 2

    When the total reaches the goal, that is, when it's at least 500, add the class reached to #saved, which makes it green.

Back

10/10

  1. 1. JavaScript in a Web Page
  2. 2. Finding Elements
  3. 3. When Nothing Is Found
  4. 4. Changing Styles
  5. 5. Reacting to Clicks
  6. 6. A Click Counter
  7. 7. Text Fields
  8. 8. Creating Elements
  9. 9. Showing an Array
  10. 10. Summary
Next

Try it yourself

Do this exercise right here in your browser. No account needed.