Events Flashcards

1
Q

What is an event?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

How is .ready() different from .load()?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

How are all events except .ready() hooked?

A

$(selector).on(‘event name’, handling function); NOTE: More than one event can be included - $(“#clickA”).on(“mouseenter mouseleave”, eventHandler);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What’s the main method to unhook an event handler?

A

$(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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

How and why do you add namespace(s) to an event?

A

$(“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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What does jQuery pass to every event handler?

A

The event object and the this object.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Define the target property of the event object.

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Define the relatedTarget property of the event object.

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Define the type property of the event object.

A

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);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Define the which property of the event object.

A

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); });

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Define the pageX and pageY properties of the event object.

A

The pageX and pageY properties hold the mouse position relative to the upper-left corner of the page.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Define the originalEvent property of the event object.

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

How do you pass data to an event

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What does .trigger() do?

A

Executes all handlers and behaviors attached to the matched elements for the given event type.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What does .on() do?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

How do you add multiple events sharing one even handler to an element?

A

$( “input” ).on(

“click change”, // Bind handlers for multiple events

function() {

console.log( “An input was clicked or changed!” );

}

);

17
Q

How do you bind multiple events with different handlers to one element?

A

Using key/value pairs:

$( “p” ).on({

“click”: function() { console.log( “clicked!” ); },

“mouseover”: function() { console.log( “hovered!” ); }

});

18
Q

How do you set up an event handler to run one function on the first event instance and a different function on all future event instances?

A

Using .one(). NOTE that if .one is bound to multiple event types then it will run once for each event type(click, scroll, etc.)

$( “p” ).one( “click”, firstClick );

function firstClick() {

console.log( “You just clicked this for the first time!” );

// Now set up the new handler for subsequent clicks;

// omit this step if no further click responses are needed

$( this ).click( function() { console.log( “You have clicked this before!” ); } );

}