1.4 - Events Flashcards
Talking about Inline events, it is true to say that:
It´s best practice or bad practice?
Bad
Why inline events are bad practice, name a few reasons
Because:
Involve writing JS as HTML attribute values
Requires JS in HTML markup
They quickly become unmanageable and inefficient
When talking about global envent handlers, we are saying that
involves assigning a function to a specific event of an element. Occurs in the JS and not in HTML
YES
What involves a Global Event Handler
Assigning a function to a specific event of an element.
Why a global event handler, is prefered over an inline event
Because:
Occurs completely in JS
Techinically is a property of an element
There is 2 ways when we work with globan event handlers
Which are them?
one is referencing the function
the other one is assingning the funciton directly on the event
If we want to fire 2 click global events on the link, how can we do it?
For example:
a console.log
and an alert()
What is the most common and preferred way to add listeners to a document?
Using event listeners
Why event listeners are the prefered way to work with events?
Because:
Does not use the “on” event naming convention
Can assign multiple functions per element
Can lookup Event listeners in Web Inspector
Can remove event listeners
How can we add an event listener?
with the method
element.addEventListener( ‘event’, theFunction, false )
With event listeners can we lookup on them in the Web Inspector?
YES
Why we need to remove events?
Because the event listener is no longer needed
To prevent memory leaks
Why can we have a memory leak when working with events?
Example: if we remove an element that has some events attached to it, it may cause memory leaks
What is the method to remove an event?
element.removeEventListener( ‘event’, theFunction, false )
el.addEventListener()
and
el.removeEventListener()
are methods of what?
methods of the DOM object
Whenever we have an event in JS, we have access to ?
The phases of that event
How can we retrieve the actual phase of an event?
with
event.eventPhase;
What are the event phases?
1 - Capturing phase
2 - Target Phase
3 - Bubbling Phase
Describe the image
Imagine clicking on the “Over the river, Charlie” text
Imagine we click on “over the River, Charlie”, in JS, in the DOM what´s happening although we don´t know that this is necessarilly going on behind the scenes but every element above the **is going to be notified during the capture phase so starting at the window level down to the document, the <html>, <body>, <table>, <tbody>, <tr>, and then finally the <td> will be notified that something beneath(debajo) has been clicked.
All this in red is part of the capture phase finally it gets to the target phase.
This explains why in the past we´ve seen event.target or e.target representing the element that was actually clicked or had the event occur. Phase 2º represent this target phase of the element that actually had the event occur.
Then, the 3º phase, the bubbling phase where every element above this(por arriba) in the DOM is again notified that this element(the clicked one) had an event occur to it so we see this 3º phase event propagation take place behind the scenes anytime we have an event on an element in the DOM.**
The Capture event phase
the number?
what is it?
how to invoke it¨?
number 1
where everything above(por arriba) the clicked element is notified, before(antes) the event.target takes place.
element.addEventListener( ‘click’, thFn, true )
The Bubbling event phase
the number?
what is it?
how to invoke it¨?
number 3
Where everything above(por arriba) is notified, after (después) the event.target has take place
element.addEventListener( ‘click’, theFn, false );
To get notified and know where the capturing and bubbling phase is taking place, we need to do what?
We need to have one or more event listeners above in the DOM where the event.target is taking place.
That´s how happend that the capture and bubbling phase listen to them.
How can we stop the event?
invoking
event.stopPropagation();
Explain what is going to console.log, and in what order in this scenario if we click on Yahoo
Todos los elementos estan escuchando por el evento ‘click’
Explain what is going to console.log, and in what order in this scenario if we click on Yahoo
Document esta siendo escuchado en la fase de captura, por lo tanto va a ser el primer elemento que se va a tomar la captura del click
Yahoo va a escuchar el click y frenar la propagación e.stopPropagation()
Los otros elementos, estan escuchando en la fase de bubble, por lo tanto no van a escuchar el evento click, porque ya lo freno el elemento yahoo
Tenemos un elemento hijo que esta escuchando por un evento ‘click’
Tenemos al elemento padre de este hijo, escuchando por un evento ‘click’
Lo que queremos hacer es capturar el evento en este padre(no importa en que fase, si captura o burbuja)
Tenemos que usar event.target?
No, we need to use the keyword .this when we are capturing the event on the parent´s elements
event.target: is the target element that initiated the event, it doesn´t change through the bubbling process
this: is the “current” element, the one that has a currently running handler on it.
When event.target and .this are equal?
When we are at the second ( 2 ) phase, the target phase.
When working with taking (capturing or bubbling phase) the event listening, what is going to be the value of this?
This is going to refer to whatever element is currently being triggered during the capturing or bubbling phases.
When removing a listener, the 3rd parameter, the useCapture parameter, must have a value of?
false
What is the name of the 3rd parameter, when calling addEventListener or removeEventListener
useCapture
The event object, contains what?
information about an event
The event object must be passed to functions, how?
must be passed to functions attached with event listeners
- el.addEventListener( ‘click’, theFn, false );*
- function theFn( e ) { }*
The event object, is often passed to functions like what parameter ( the name of the parameter)
like e
theFn( e ) { }
In the event object, the available properties will depende on what factors?
on the type of event, that we are listening!
if it´s a click, load, mouseover event, etc…
All browsers display the same properties on the event object?
No, there are some cross browser differences