Command Palette

Search for a command to run...

Hectal
Back to the lesson: Topic 9.5 — Mouse, Keyboard and Focus Events
JavaScript · Example 4 of 4

Focus, blur and focusin on a real form

Click into a field, then press Tab and Shift+Tab. Leaving a field empty turns it red. relatedTarget names the field focus came from.

Pointer events (pointerdown, pointerup, click, dblclick) cover mouse, touch and pen; keyboard events (keydown, keyup) give you key (the character meant) and code (the physical key); and focus events (focus, blur, focusin, focusout) tell you which element will receive typing.

Change the code and press Run (Ctrl+Enter). Predict what will happen first, then break it on purpose and read what changes. Your edits are saved and match the lesson page.

Ready for more? This phase has 6 hands-on exercises with automatic checks.

Practice questions

Write the code in the editor, run it, then open the model answer to compare.

01

Make arrow keys move a 30 px square #player 10 px at a time (left and right), using transform. Test with two dispatched ArrowRight presses and log the position.

02

Show a #tip paragraph while a <input id="pwd"> has focus, and hide it when focus leaves.

Explain it without notes

01

What is the difference between event.key and event.code? When would you use each?

02

Why are pointer events preferred over mouse and touch events?

03

What is the difference between focus/blur and focusin/focusout?

04

Which events fire, in order, when a user double-clicks a button?

Focus, blur and focusin on a real form

Preview

Press Run to see the page.

Console

Nothing logged yet.