Jump to content

DOM event handling


setun-90
 Share

Recommended Posts

After reading about things such as event.currentTarget and a discussion with Ingolme, I'm now having serious doubts about my understanding of DOM.

 

My basic question is: are DOM event handlers called synchronously or asynchronously?

Link to comment
Share on other sites

You were right in your previous post that your code behaved differently than it would all in a single function. I had not noticed at first that the set() function was being called once for each level of nesting as opposed to only once for each click event.

 

event.currentTarget is a reference to the actual element that the event listener was attached to, as opposed to event.target, which is a reference to the lowest leaf node that was actually clicked on.

 

Events are asynchronous, they don't run along with the rest of the code on the page, only when an event is fired.

Link to comment
Share on other sites

Right, so would it be possible to write a function in the linked script that could 'see' event.currentTarget change, either across function calls or within a call? Cause then I wouldn't need to attach handlers or listeners to any descendant of #nav.

Link to comment
Share on other sites

The event object only gets passed to an event handler. But you could put an event handler on the parent, and then use currentTarget (or target) to see which element actually fired the event. Look into event bubbling versus capturing.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...