JavaScript - Events Flashcards
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.
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.
JavaScript’s main purposes is to add ________ to your pages: the user does something and the page reacts. Events help accomplish this purpose.
JavaScript’s main purposes is to add interactivity to your pages: the user does something and the page reacts. Events help accomplish this purpose.
Types of Events:
click mouseenter mouseleave keydown,keyup window.load (gets triggered when the page is fully loaded) dragover ondrop
Attaching as an attribute directly on an element in html
<p>Click here</p>
Attaching as a property on an element in JavaScript
targetElement.onclick = function(){
};
Using the addEventListener syntax - preferred
element.addEventListener('click', handlerFunction, false); function handlerFunction(e){ console.log(e); console.log(e.type); console.log(e.target); }
The object “e” refers to the _____ object that was created when the _____ occurred.
The object “e” refers to the event object that was created when the event occurred.
e.target refers to the _______ that triggered the _______.
e.target refers to the element that triggered the event.
What happens behind the scenes of register/listen?
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
Targeting by using name attributes:
var targetElement = document.forms.formName.elementName;
Targeting by using getElementById:
var targetElement = document.getElementById(formElementId);
This table shows common element types and properties used to retrieve values and states
Element Property input type="text" .value input type ="checkbox" .checked input type="radio" .checked select .selectedIndex
We can access an input value using the following code:
We can access an input value using the following code:
console.log(targetInput.value)
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.
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.
Once form values have been retrieved you can use JavaScript to check if the input is _____. Usually, this _______ is performed when the form is ______.
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.
When validating a form, remember to use “________;” in the form’s submit handler to prevent a form from being submitted to its “______” page.
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.
Use _____._____(); to submit a form using Javascript.
Use formElement.submit(); to submit a form using Javascript.
Form elements may be validated using an element’s “______” event that gets triggered when an element’s value is _____.
Form elements may be validated using an element’s “change” event that gets triggered when an element’s value is changed.
Other common events used with form elements:
blur
focus
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 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.
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 _______.
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.
Timeout Example:
var timeoutId = window.setTimeout(ticker, 2000); function ticker(){ console.log("tick"); }
Interval Example:
var intervalId = window.setInterval(ticker, 2000); function ticker(){ console.log("tick"); }
Clear timeouts and intervals:
clearTimeout(timeoutId);
clearInterval(intervalId);