Events Flashcards
What is an event?
An event is the occurrence of anything the system considers significant. It can originate in the browser, the form, the keyboard, or any other subsystem, and it can also be generated by the application via a trigger. An event can be as simple as a key press or as complex as the completion of an Ajax request.
How is .ready() different from .load()?
The ready event is similar to the document load event except that it doesn’t wait for all of the page’s images and other assets to load. It only waits for the DOM to be ready. Also, if the ready event fires before it is hooked, the handler code will be called at least once, unlike most events.
How are all events except .ready() hooked?
$(selector).on(‘event name’, handling function); NOTE: More than one event can be included - $(“#clickA”).on(“mouseenter mouseleave”, eventHandler);
What’s the main method to unhook an event handler?
$(elements).off(‘event name’, handling function); NOTE: The handling function is optional, and the event name is optional as well. If the event name is omitted, then all events attached to the elements are removed. If the event name is included, then all handlers for the specified event are removed.
How and why do you add namespace(s) to an event?
$(“button”).on(“click.alpha”, handler); – OR – $(“button”).on(“click.alpha.beta”, handler); They allow us to distinguish between different handlers for the same event without using the handler function
What does jQuery pass to every event handler?
The event object and the this object.
Define the target property of the event object.
This is the element that triggered the event. This is not the same thing as the element bound to the event handler (the one pointed to by the this object). For example, if you click an tag, which doesn’t have a handler, but its parent,
, does, the event bubbles up to the parent. Under these conditions, event.target points to the tag, but the this object points to the
element.
Define the relatedTarget property of the event object.
The relatedTarget property also points to an element when valid, but rather than being the element that triggered the event, it is an element that is somehow related to the event instead.
Define the type property of the event object.
This property holds the name of the current event. It could come in handy if you use a single event handler for multiple events: function eventHandler(event) { console.log(“event type = “ + event.type); } $(“#clickA”).on(“mouseenter”, eventHandler); $(“#clickB”).on(“mouseleave”, eventHandler);
Define the which property of the event object.
When a mouse or keyboard event occurs, this property can be used to tell which button or key was pressed. Let’s take a quick look at a code sample: $(“#itemName”).on(‘keypress’, function (event) { console.log(“key type: “ + event.which); });
Define the pageX and pageY properties of the event object.
The pageX and pageY properties hold the mouse position relative to the upper-left corner of the page.
Define the originalEvent property of the event object.
When an event occurs, jQuery normalizes it so that the events in every browser behave in the same manner. Occasionally, jQuery’s normalized event object lacks something that the original event object had and your application needs. jQuery places a complete copy of the original event object in the originalEvent property exactly for this reason.
How do you pass data to an event
All you need to do is pass the data after the event name when hooking the event. You can pass nearly any type of data with a few caveats. First, if the data is a string, then you must also set the optional selector parameter that precedes it in the parameter list. If you don’t need the selector parameter, you can set it to null. Second, the data that you pass can’t be null or undefined.
What does .trigger() do?
Executes all handlers and behaviors attached to the matched elements for the given event type.
What does .on() do?
Attach an event handler function for one or more events to the selected elements. Shorthand methods(.click, .scroll, .mouseenter, etc) are convenient substitutes for .on, but are not sufficient for A) binding the same handler function to multiple events, B) providing data to the event handler, C) working with custom events, or D) passing an object of multiple events and handlers.