Ugrás a fő tartalomra
A Frontend ingyenes bónusz a CodeMentor AI-n — a fő termék a Python. Regisztrálj a haladásod mentéséhez és nyisd meg mind a 299 Frontend leckét + 1,100+ Python leckét.Regisztrálj — ingyen
← ⚡ JavaScript & the browser·Modul C1 · Lecke 5
FeladatBuild the counter. HTML has <button id='dec'>−</button> <span id='count'>0</span> <button id='inc'>+</button>. In <script>: declare let count = 0, query the three elements, set count's textContent on load, and wire inc/dec buttons to update count and re-render. After clicking inc twice in the embedded preview, the console should log 'count: 2' (use console.log on every re-render).

Modul záróköve: működő számláló widget

200 XP12 minFREE
Elmélet

Putting C1 together

You've seen variables, types, template literals, arrays. Time to wire them into something interactive.

A counter widget is the "hello world" of UI work:

  1. A page element shows a number.
  2. A "+" button increments it.
  3. A "−" button decrements it.

Three pieces:

let count = 0;                                    // state
const out = document.getElementById("count");     // DOM reference
out.textContent = count;                          // initial render

document.getElementById("inc").addEventListener("click", () => {
  count += 1;
  out.textContent = count;
});

You'll see document.getElementById and addEventListener everywhere from here on. C2 covers them in depth — for now treat them as "find an element by id" and "run code when something happens."

The "render after change" rule

Every time count changes, you re-render. Forgetting to re-render is the #1 newbie bug: the value in memory updates, but the screen doesn't change because no one told the DOM. Re-rendering after every state change keeps you out of that trap.

This is the same idea React, Vue and Svelte automate for you. Here you do it by hand — and once you've done it ten times, the frameworks make a lot more sense.

2 lapok
Élő előnézet
console output appears here…
← ElőzőKövetkező lecke →

Egy Python- vagy webes tipp naponta — emailben

Rövid, kézzel írt, spam nélkül. Egy kattintással leiratkozhatsz.