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’























