1

i'm really confused about the fs function. i mean i don't understand when i should use the function parameter,function parameter.target and when this.For example this code:

  e.target.addEventListener('mouseout',function handler(d)
        {   
            var divE=d.target.parentNode.querySelector('div.preview');
            divE.parentNode.removeChild(divE);
            e.target.removeEventListener('mouseout',handler,false);
        },false);

why is used e.target and neither e nor this?

1
  • this is the element that addEventListener was called on. e.target is the (child) element that the event was triggered on. They may be the same, they may be different. e is an event object. Commented May 22, 2014 at 16:08

1 Answer 1

1

The e.target is typically used when delegating an event handler to an element that has dynamic children or descendants. Using delegation, you need to check e.target to find out which descendant element triggered the event, because the event has bubbled up from the descendant.

Delegation ins't just used when you have dynamic elements. It's also useful when for example, you have lots of elements which all need an event handler. In that case you might prefer to delegate to the parent element just once, instead of assigning the event listener to all descendants.

The this context is the element that has the event listener assigned, so if you aren't doing delegation then this can be used.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.