Nearby lessons

9 of 14

JavaScript HTML DOM Events

📌 What are DOM Events?

JavaScript DOM events let you execute code when users interact with your webpage. Events can be assigned using HTML attributes or JavaScript. Common events include click, input, mouseover, focus, and more.

🔗 HTML Event Attributes

You can directly assign events using HTML attributes such as onclick, onmouseover, etc.

Example02
🧩Code Cell
Loading Editor...
✏️ You can edit this code — click Run to refresh the preview.
Output

📌 Assign Events Using JavaScript DOM

Instead of using HTML attributes, you can assign events with JavaScript using element.addEventListener() or element.event = function.

Example03
🧩Code Cell
Loading Editor...
✏️ You can edit this code — click Run to refresh the preview.
Output

🔃 onload and onunload Events

onload runs when the page or element finishes loading. onunload runs when the user leaves the page.

Example04
🧩Code Cell
Loading Editor...
✏️ You can edit this code — click Run to refresh the preview.
Output

⌨️ oninput Event

Triggered every time a user types or changes input in a field.

Example05
🧩Code Cell
Loading Editor...
✏️ You can edit this code — click Run to refresh the preview.
Output

📝 onchange Event

Fires when the input loses focus and its value has changed (for input, select, textarea).

Example06
🧩Code Cell
Loading Editor...
✏️ You can edit this code — click Run to refresh the preview.
Output

🖱️ onmouseover and onmouseout Events

onmouseover triggers when the mouse enters the element, onmouseout when it leaves.

Example07
🧩Code Cell
Loading Editor...
✏️ You can edit this code — click Run to refresh the preview.
Output

🖱️ onmousedown, onmouseup, and onclick

Use onmousedown when the mouse button is pressed, onmouseup when released, and onclick for a full click action.

Example08
🧩Code Cell
Loading Editor...
✏️ You can edit this code — click Run to refresh the preview.
Output

🟡 onfocus Event

The onfocus event runs when an element (like input) gains focus.

Example09
🧩Code Cell
Loading Editor...
✏️ You can edit this code — click Run to refresh the preview.
Output

🧠 Test Your Knowledge

3 Questions
Progress: 0 / 3