← Livello 2 – Mago

Lezione 2 di 8 ⏱ 25 minuti

Il tuo primo incantesimo: un pulsante che fa qualcosa

La pagina reagisce a un clic per la prima volta. Imparerai a trovare un elemento e ascoltare gli eventi.

Te lo mostro: i tre passi di ogni incantesimo

Quasi tutto ciò che JavaScript fa su una pagina segue tre passi:

  1. Trova un elemento sulla pagina (un pulsante, un titolo, un’immagine…),
  2. ascolta un evento (un clic, passaggio del mouse, digitazione…),
  3. fai qualcosa (cambia il testo, il colore, nascondilo, mostralo…).

Ecco com’è nel codice. Nell’HTML, aggiungi un pulsante e un paragrafo:

<button id="my-button">Click me</button>
<p id="greeting">Nothing has happened yet.</p>

E in script.js:

// 1. Find the elements by their id
const myButton = document.getElementById("my-button");
const greeting = document.getElementById("greeting");

// 2. Listen for a click on the button
myButton.addEventListener("click", () => {
  // 3. Do something: change the paragraph's text
  greeting.textContent = "The spell works! 🎉";
});

Analizziamo le novità:

Ora tocca a te 💪

Aggiungi il tuo primo incantesimo al sito web. Scegline uno in base al progetto (oppure inventane uno tuo):

🏫 Sito web della scuola – pulsante "Cosa c'è in mensa oggi?"

HTML (sulla homepage):

<button id="lunch-button">What's for lunch today?</button>
<p id="lunch"></p>

script.js:

const lunchButton = document.getElementById("lunch-button");
const lunch = document.getElementById("lunch");

lunchButton.addEventListener("click", () => {
  lunch.textContent = "Today: pizza and salad! 🍕";
});
🙋 Sito web personale – pulsante "Raccontami una barzelletta"
<button id="joke-button">Tell me a joke</button>
<p id="joke"></p>
const jokeButton = document.getElementById("joke-button");
const joke = document.getElementById("joke");

jokeButton.addEventListener("click", () => {
  joke.textContent = "Why do programmers prefer dark mode? Because light attracts bugs! 😄";
});
🌍 Diario di viaggio – pulsante "Dove andiamo dopo?"
<button id="where-button">Where are we going next?</button>
<p id="where"></p>
const whereButton = document.getElementById("where-button");
const where = document.getElementById("where");

whereButton.addEventListener("click", () => {
  where.textContent = "Packing our bags: we're headed to the beach in Greece! 🌊";
});
⚽ Sito web del club – pulsante "Quando è la prossima partita?"
<button id="match-button">When's the next match?</button>
<p id="match"></p>
const matchButton = document.getElementById("match-button");
const match = document.getElementById("match");

matchButton.addEventListener("click", () => {
  match.textContent = "Saturday at 10:00, home game vs. Riverside. Come cheer us on! 📣";
});

Bonus: stila il pulsante in CSS così è bello:

button {
  font-size: 16px;
  font-weight: bold;
  padding: 10px 20px;
  border: none;
  border-radius: 999px;
  background-color: #1d4ed8;
  color: white;
  cursor: pointer;
}

button:hover {
  background-color: #1e40af;
}
⚠️

Non funziona? Apri la console (F12). Gli errori più comuni: 1) l’id in HTML non corrisponde esattamente al nome in getElementById (conta anche maiuscole/minuscole), 2) il <script> non è proprio alla fine di <body>, 3) una parentesi mancante – la console ti dirà il numero di riga.

Controlla da solo: Clicchi il pulsante e compare testo sulla pagina. Hai appena collegato tutte e tre le lingue del web: HTML (il pulsante), CSS (l’aspetto) e JavaScript (il comportamento).

Cosa porti via da questa lezione