Chapter 6: Events Flashcards
What are examples of UI events
load unload error resize scroll
What are examples of keyboard events
keydown
key up
keypress
What are examples of mouse events
click dblclick mousedown mouseup mousemove mouseover mouseout
What are examples of focus events
focus / focusing
blur / focusout
What are examples of form events
input change submit reset cut copy paste select
What are mutation events
These occur when the DOM structure of a page changes by a script (to be replaced by mutation observers)
What do we mean by saying an event fired/raised?
It happened.
What do we mean by events trigger scripts
events find the specified script to run
What are the three ways to bind an element?
HTML events handlers (nowadays bad practice), dom event handlers and dom level 2 event listeners
What advantages does event listeners have over DOM event handlers?
Allows you to have multiple functions linked to one event.
Event listers are no support in IE8 and under.
What’s the syntax for HTML event handlers?
NB: We don’t write these nowadays
these are written as an element attribute and generally preceded by the word on
How do you write a DOM event handler
you select the element that you want to attach the code to. then attach the event plus the code to run
e. g.
element. onevent = functionName;
switch.onclick = replaceAll;
Notes: when attaching a function we don’t call it, we’re a assigning it. You can only attach one function to an event handler
How do we write an event listener
we use the addEvenetListener method to an element. This takes three parameters, the event, a function, a boolean
e. g. element.addEventListener(‘event’, functionName, Boolean)
el. addEventListener(‘blur’, checkName, false);
How do you pass parameters to functions used for an event?
You need a work around and that’s to wrap the function in anonymus function
elUsername.addEventListener(‘blur’, function(){
checkUsername(5);
}, false);
How do we support IE5-8 that don’t use addEventListerner
They have an alternative method called attachEvent
So we can write an if else
if (elUsername.addEventListerner) { elUsername.addEventListener('blur', function(){ checkUsername(5); }, false); } else { elUsername.attachEvent('onblur', function(){ checkUsername(5); } ); }