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.