HTML CSS Bootstrap JavaScript jQuery MySQL PHP Data Mining

HTML Event Reference

Global Event Attributes are HTML attributes that can be added to almost any HTML element. They are used to execute JavaScript code when a specific "event" occurs (such as a user clicking a button, hovering over a picture, or a page finishing loading).


Window Event Attributes

These events fire for the window object, meaning they apply to the entire browser window/tab, rather than a specific text paragraph or image. They usually belong on the <body> tag.

Attribute Description
onafterprintScript to be run after the document is printed
onbeforeprintScript to be run before the document is printed
onerrorScript to be run when an error occurs while loading an external file
onhashchangeScript to be run when there has been changes to the anchor part of a URL
onloadScript to be run when an element is completely loaded entirely
onofflineScript to be run when the browser starts to work offline
ononlineScript to be run when the browser starts to work online
onresizeScript to be run when the browser window is resized
onunloadScript to be run when the user navigates away from the page

Form Event Attributes

These events fire by actions taking place inside HTML Forms (like typing in input fields, clicking submit, or changing a dropdown selection).

Attribute Description
onblurFires the moment that a form element loses focus (the user clicks somewhere else)
onchangeFires the moment the value of the element changes (great for dropdowns)
oncontextmenuScript to be run when a context menu is triggered (user right-clicks)
onfocusFires the moment an element gets focus (the user clicks into the text box)
oninputScript to be run immediately when an element gets user input (triggers exactly on keystroke)
oninvalidScript to be run when an element is considered invalid based on HTML built-in validation
onresetFires when the Reset button in a form is clicked
onsearchFires when the user writes something in a search field (for <input type="search">)
onsubmitFires when a form is naturally submitted

Keyboard Event Attributes

These events fire based on physical keyboard interactions by the user.

Attribute Description
onkeydownFires the exact moment a user is physically pressing a key down
onkeypressFires when a user presses a key (deprecated in modern JavaScript, use onkeydown instead)
onkeyupFires the exact moment the user physically releases a key

Mouse Event Attributes

These events fire when the user interacts with their mouse or trackpad.

Attribute Description
onclickFires when the user clicks cleanly on an element
ondblclickFires when the user double-clicks on an element
onmousedownFires the exact moment a mouse button is pressed down over an element
onmouseenterFires when the mouse pointer is moved directly onto an element
onmouseleaveFires when the mouse pointer is moved out of an element
onmousemoveFires continuously as the mouse pointer is moving while it is over an element
onmouseoverSimilar to onmouseenter, but it also triggers when entering child elements
onmouseoutSimilar to onmouseleave, but it also triggers when leaving child elements
onmouseupFires the exact moment a mouse button is released over an element
onwheelFires when the mouse wheel rolls up or down over an element

Drag Event Attributes

These events correspond to the HTML5 Drag and Drop API.

Attribute Description
ondragScript to be run when an element is physically being dragged
ondragendScript to be run exactly at the end of a drag operation
ondragenterScript to be run when a dragged element boldly enters a valid drop target area
ondragleaveScript to be run when a dragged element leaves a valid drop target area
ondragoverScript to be run constantly while a dragged element is hovering over a valid drop target area
ondragstartScript to be run strictly at the start of a drag operation
ondropScript to be run when dragged element is released (dropped) onto a drop target
onscrollScript to be run when an element's scrollbar is being scrolled
Modern Best Practice: While embedding events directly into HTML as attributes (like <button onclick="myFunc()">) is completely valid and great for beginners, professional JavaScript developers usually prefer to attach events completely remotely using addEventListener() inside their JS files instead, keeping the HTML perfectly clean.