user3629077
user3629077

Reputation: 11

the cause for using javascript parameter.target

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?

Upvotes: 1

Views: 407

Answers (1)

MrCode
MrCode

Reputation: 64526

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.

Upvotes: 1

Related Questions