← المستوى 2 – الساحر

الدرس 2 من 8 ⏱ 25 دقيقة

أول تعويذة لك: زر يفعل شيئاً

الصفحة تتفاعل مع النقرة لأول مرة. ستتعلّم إيجاد عنصر والاستماع للأحداث.

شاهد معي: ثلاث خطوات لكل تعويذة

거의 كل ما يفعله JavaScript على الصفحة يتبع ثلاث خطوات:

  1. أوجد عنصراً على الصفحة (زر، عنوان، صورة…)،
  2. استمع لحدث (نقرة، مرور الفأرة، كتابة نص…)،
  3. افعل شيئاً (غيّر النص، اللون، أخفِ، أظهر…).

هكذا يبدو ذلك في الكود. في HTML، أضف زراً وفقرة:

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

وفي 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! 🎉";
});

لنفكّك ما هو جديد:

دورك الآن 💪

أضف أول تعويذة إلى موقعك. اختر واحدة حسب مشروعك (أو اخترع واحدة):

🏫 موقع المدرسة – زر «ماذا على الغداء اليوم؟»

HTML (على الصفحة الرئيسية):

<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! 🍕";
});
🙋 موقع شخصي – زر «احكِ لي نكتة»
<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! 😄";
});
🌍 يوميات السفر – زر «إلى أين نذهب بعد ذلك؟»
<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! 🌊";
});
⚽ موقع النادي – زر «متى المباراة التالية؟»
<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! 📣";
});

مكافأة: زيّن الزر في CSS ليبدو جميلاً:

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;
}
⚠️

لا يعمل؟ افتح console (F12). الأخطاء الأشيع: 1) الـ id في HTML لا يطابق الاسم في getElementById تماماً (حتى حالة الأحرف مهمة)، 2) وسم <script> ليس في نهاية <body> مباشرة، 3) قوس ناقص – الـ console يخبرك برقم السطر.

✅

تحقق من نفسك: تضغط الزر ويظهر نص على الصفحة. لقد وصلت للتو اللغات الثلاث للويب: HTML (الزر)، CSS (الشكل)، وJavaScript (السلوك).

ما ستخرج به من هذا الدرس