JavaScript - Events Flashcards

1
Q

An event is something that _______ in the ______ (in our case, on our page). We can instruct our code to listen for _______, then execute code when that ______ occurs.

A

An event is something that happens in the application (in our case, on our page). We can instruct our code to listen for events, then execute code when that event occurs.

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

JavaScript’s main purposes is to add ________ to your pages: the user does something and the page reacts. Events help accomplish this purpose.

A

JavaScript’s main purposes is to add interactivity to your pages: the user does something and the page reacts. Events help accomplish this purpose.

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

Types of Events:

A
click
mouseenter
mouseleave
keydown,keyup
window.load (gets triggered when the page is fully loaded)
dragover
ondrop
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Attaching as an attribute directly on an element in html

A

<p>Click here</p>

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

Attaching as a property on an element in JavaScript

A

targetElement.onclick = function(){

};

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

Using the addEventListener syntax - preferred

A
element.addEventListener('click', handlerFunction, false);
function handlerFunction(e){
	console.log(e); 
	console.log(e.type);
	console.log(e.target); 
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

The object “e” refers to the _____ object that was created when the _____ occurred.

A

The object “e” refers to the event object that was created when the event occurred.

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

e.target refers to the _______ that triggered the _______.

A

e.target refers to the element that triggered the event.

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

What happens behind the scenes of register/listen?

A

Register/listen for “click” event on an element
User clicks that element
Browser creates event object, also called “e”, and calls event handler function
Event Handler gets triggered with the event object as a parameter
Event handler code runs
Forms

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

Targeting by using name attributes:

A

var targetElement = document.forms.formName.elementName;

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

Targeting by using getElementById:

A

var targetElement = document.getElementById(formElementId);

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

This table shows common element types and properties used to retrieve values and states

A
Element	Property
input type="text"	.value
input type ="checkbox"	.checked
input type="radio" .checked
select .selectedIndex
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

We can access an input value using the following code:

A

We can access an input value using the following code:

console.log(targetInput.value)

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

The .______ property works for other input elements including color picker, date, range and even checkbox and radio buttons. The select element also has a ______ property.

A

The .value property works for other input elements including color picker, date, range and even checkbox and radio buttons. The select element also has a value property.

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

Once form values have been retrieved you can use JavaScript to check if the input is _____. Usually, this _______ is performed when the form is ______.

A

Once form values have been retrieved you can use JavaScript to check if the input is valid. Usually, this validation is performed when the form is submitted.

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

When validating a form, remember to use “________;” in the form’s submit handler to prevent a form from being submitted to its “______” page.

A

When validating a form, remember to use “return false;” in the form’s submit handler to prevent a form from being submitted to its “action” page.

17
Q

Use _____._____(); to submit a form using Javascript.

A

Use formElement.submit(); to submit a form using Javascript.

18
Q

Form elements may be validated using an element’s “______” event that gets triggered when an element’s value is _____.

A

Form elements may be validated using an element’s “change” event that gets triggered when an element’s value is changed.

19
Q

Other common events used with form elements:

A

blur

focus

20
Q

A timeout is essentially a ______. When the countdown is complete, a ______ will be called. The timeout expires when the countdown is ________ or when the author has decided to _____ it.

A

A timeout is essentially a countdown. When the countdown is complete, a function will be called. The timeout expires when the countdown is complete or when the author has decided to remove it.

21
Q

An interval, like a timeout will execute a ______ after a specified period of time. However, it will continue to execute that ______ (with the specified delay between function calls) until it is _______.

A

An interval, like a timeout will execute a function after a specified period of time. However, it will continue to execute that function (with the specified delay between function calls) until it is canceled.

22
Q

Timeout Example:

A
var timeoutId = window.setTimeout(ticker, 2000);
function ticker(){    
	console.log("tick"); 
}
23
Q

Interval Example:

A
var intervalId = window.setInterval(ticker, 2000); 
	function ticker(){    
	console.log("tick"); 
}
24
Q

Clear timeouts and intervals:

A

clearTimeout(timeoutId);

clearInterval(intervalId);