🐞 Real-World Bug: “Button Click Not Working
🔧 The Code:
🧠 Step-by-Step Debugging:
🔍 Step 1: Read the error
Open the browser console. You may see something like:
🧩 Step 2: Reproduce the bug
Clicking the button does nothing. The alert
doesn’t appear.
🔎 Step 3: Investigate
Check this line:
Now check the HTML:
🛑 Problem: ID is case-sensitive
-
clickme
≠clickMe
✅ Step 4: Fix it
Update the code:
Now clicking the button works. 🎉
🔄 Advanced Debug Tip: DOM Not Ready
If you moved the script to the head section or used defer=false
, this can also cause the error.
Better Approach:
Wrap your code in DOMContentLoaded
:
💡 Key Lessons from This Debugging Example
Lesson | Why It Matters |
---|---|
ID case sensitivity | JavaScript is case-sensitive |
DOM must be loaded | Always access elements after they exist |
Use console errors | Always check browser dev tools |
Small typo = Big bug | Carefully check spelling & IDs |