Events
Events Overview
- Browsers have the ability to actively notify our code that an event occurs. This is done by registering functions as handlers
- Example1:
window.addEventListener("click", () => {enter code here which will be performed after the window is clicked....}
- window binding refers to a built-in object provided by the browser. It represents the browser window that contains the document
- Calling addEventListener method registers the second argument to be called whenever the event described by it's first argument occurs.
- Example2:
let button = document.querySelector("button");
button.addEventListener("click", () =>{enter code here to be performed after the button is clicked};
- Event handler functions are passed an argument, the event object
- Example: The button object type property holds a string identifying the event (ie/ click or mousedown, ect.)
How to Connect Your Code to an Event
You can connect your code to an event in three ways:
- Assigning HTML attributes
Downsides to using HTML attribute event handlers
- HTML and JavaScript are mixed together which can make it harder to maintain and debug
- You can't remove an event handler without changing HTML
- You can only set up event handlers for elements that appear in your HTML code, as opposed to elements you create dynamically, such as using document.createElement()
- Assigning an object's special properties
Example:
- document.getElemendById("someID").onclick = functionName;
- Downside is you can only assign one function to handle a given event
- Calling an object's special methods
- This is the standard DOM event model
- document.getElemendById("someID").addEventListener("click", functionName);
- An advantage is you can register multiple listeners and handlers
- Methods:
- removeEventListener("eventType", elementObjClick)
Common HTML Events
- onchange - An HTML element has been changed
- onclick - The user clicks an HTML element
- onmouseover - The user moves the mouse over an HTML element
- onmouseout - The user moves the mouse away from an HTML element
- onkeydown - The user pushes a keyboard key
- onload - The browser has finished loading the page
Attributes
- onclick - You can attach one handler to the attribute whose name is the event name with on in front of it
- tabindex -
Event Methods
- click - occurs when a mouse button is clicked (pressed and released)
- dblclick -
- keydown -
- keyup -
- mousedown - occurs when a mouse button is pressed
- mouseup - occurs when a mouse button is released
- mouseover - occurs when a mouse button is moved onto an element or text
- mouseout - occurs when a mouse button is moved out and away from an element or text
- mousemove - This event can be used to track the position of the mouse
- clientX and clientY properties contain event's coordinates (in pixels) relative to top-left corner of the window
- pageX and
pageY properties contain event's coordinates (in pixels) relative to top-left corner of the whole document (which may be different when the window has been scrolled)
- touchstart - When a finger starts touching the screen
- touchmove - When a finger is moved while touching a screen
- Since many touchscreens can detect multiple fingers at once, these events do not have single set coordinates, rather their event objects have touches property, which holds an array-like object of points
- scroll - When an element is scrolled
- focus - event is fired when an element gains focus
- blur - event is fired when an element loses focus
- load - event is fired when a page finishes loading. It fires on the window and the document body objects. This is often used to schedule initialization actions that require the whole document to have been built.
- beforeunload - event is fired whe a page is closed or navigated away from. The main use is to prevent the user from accidentally losing work by closing a document.
Methods
- addEventListener - method is used to register an event handler to make it p ossible to detect and react to events happening in a web page
- removeEventListener
- stopPropagation - prevents handlers further up (parent node and higher to the window) from receiving the event
- preventDefault - method is called to prevent the default behaviour of the handler being called before the default behaviour takes place
Properties
- target property refers to the node where it originated, the element object that received the event
- type property can be used to determine what type of event occurred
- bubbles property indicates whether an event can bubble, which is passing control from one element to another up the hierarchy
- cancelable property indicates whether an event can have its default action canceled
- currentTarget property identifies the current target for the event as the event traverses the DOM
- defaultPrevented property indicates whether or not preventDefault() has been called on the event
- eventPhase property indicates which phase of the event flow an event is in
- timestamp property indicates at what time the event occurred
Propagation
- Handlers usually register on nodes with children also receiving the events. For example, if a button inside a paragraph is clicked, event handlers on the paragraph will also see the click event. However, if the paragraph and button both have a handler, the more specifc handler gets to go first. The event is said to propagate outward
Event Example