JavaScript for WordPress › Forums › Vanilla JavaScript › 1.4.6 – Event Propagation – Simple question about .id
- This topic has 7 replies, 2 voices, and was last updated 7 years, 4 months ago by Zac Gordon.
-
AuthorPosts
-
May 17, 2017 at 5:36 pm #27570brunoParticipant
Hi!
I have another question, in this video in 4:55 it shows the example of calling directly
this.id
Is witch a shorthand for calling getElementById? Right? or I misunderstood that?
May 17, 2017 at 5:50 pm #27575Zac GordonKeymasterActually,
this.id
is shorthand for
this.getAttribute( 'id' )
documentGetElementById( ‘X’ ) will select an element on the page with an ID assigned of ‘X’. Once you get that value, .id would give you the value X.
So something like this:
var el = document.getElementById( 'content' ); console.log( el.id ); // Prints 'content'
Hope that helps! Good question 🙂
May 17, 2017 at 5:59 pm #27577brunoParticipantTx!
I have another question!
I’m trying to comprehend the event propagation, and I have like a conceptual question:
I understand what is happening with the capturing and bubbling phase, what I don´t fully understand is what we are doing on the 3º parameter on the addEventListener method like:
el.addEventListener( 'click', theFunc, false); el.addEventListener( 'click', theFunc, true);
when we set false, or true on that parameter, what we are saying is basically “how the notification of the event will occur, if after or before the event target happens?”
something like that? Maybe it’s a messy quesiton, and I´m not doing it correctly, let me know.
May 17, 2017 at 6:13 pm #27579Zac GordonKeymasterThat is correct. The third parameter ‘useCapture’ determines whether or not the capturing phase is used.
el.addEventListener( 'click', theFunc, false); // Uses Bubbling el.addEventListener( 'click', theFunc, true); // Uses Capturing
It’s also helpful to know that if you don’t pass a third parameter is will use false by default and leverage bubbling.
This can be a tricky topic, but hope that helps!!!
May 17, 2017 at 6:38 pm #27581brunoParticipantAnother quesiton:
In the image at the video, it shows all the process of the capturing and bubbling phases
In the example with the code, it shows that the event phases starts at the parent element.
So the first image that starts the capturing and bubbling phase, and has the window object confuse me a little bit.
So when we are going to capture the event, were did it start? at the parent element, or at the window object?
Tx, I´m really enjoying the course, and learning a lot!
May 17, 2017 at 6:53 pm #27585Zac GordonKeymasterIt always starts at the window level. But if you don’t have an event listener on the window you won’t notice it.
In our example we have a listener attached to the parent element so that is where it ‘starts’ for us because that is the highest element in the tree we have a listener attached to.
However, you could attach a listener to any element higher in that chain and have the same effect.
Hope that explains it? Let me know if it’s still unclear.
So happy to hear you’re enjoying the course!!!!!
May 18, 2017 at 3:33 pm #27609brunoParticipantI think it starts to be clear
So if we have another event listener on the body for example, we can be notified when the capturing phase or bubbling phase came across the body element?
May 18, 2017 at 3:43 pm #27613Zac GordonKeymasterIf you want to use bubbling then you would need an event listener on the target with the third parameter set to true. In the examples you can see we play around with attaching both bubbling and capturing events.
-
AuthorPosts
- You must be logged in to reply to this topic.