Summary
A common rookie mistake is trying to pass the clicked DOM element to an inline onclick handler without actually supplying it. This leads to undefined errors and makes the scaling logic impossible to apply to the correct image.
Root Cause
- The inline handler is written as
onclick="script()"but the function expects an argument (img). - No reference to
this(the clicked element) is passed, so the function receivesundefined. - The CSS transform string is malformed (
'50px'is not a validtransformvalue).
Why This Happens in Real Systems
- Developers rely on quick inline event handlers while learning, forgetting that the handler must receive context.
- Mixing HTML attribute event wiring with modern DOM API leads to mismatched expectations.
- Lack of a shared utility (e.g., a “zoom” class) means each developer rewrites the same logic, increasing the chance of small syntax bugs.
Real-World Impact
- User‑visible bugs: clicking the image does nothing or throws a console error, breaking the UI.
- Maintenance overhead: other team members must debug the undefined reference each time they add a new image.
- Performance loss: inline handlers prevent the browser from efficiently delegating events, especially in large galleries.
Example or Code (if necessary and relevant)

.zoomed {
transform: scale(1.5);
transition: transform 0.2s ease;
}
function toggleZoom(img) {
img.classList.toggle('zoomed');
}
How Senior Engineers Fix It
- Use event delegation: attach a single listener to a parent container instead of per‑image inline handlers.
- Pass the event object and derive the target (
event.target) to keep code decoupled. - Encapsulate scaling logic in a reusable CSS class rather than manipulating
stylestrings. - Validate inputs and guard against non‑image elements.
document.querySelector('.imggallery').addEventListener('click', function (e) { const img = e.target.closest('img'); if (!img) return; img.classList.toggle('zoomed'); });
Why Juniors Miss It
- Focus on immediate visual result rather than understanding the event model.
- Unfamiliarity with
thisin inline handlers and the difference between attribute strings and function parameters. - Little exposure to progressive enhancement patterns like event delegation and CSS‑driven state.