Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

event.target.id VS event.currentTarget.id VS this.id

I have this sample code provided below:

HTML:

<button id = '33' class = "clickme">Click here</button>

JS:

$(document).on("click",".clickme",function(event){ 
    var eti = event.target.id;
    var eci = event.currentTarget.id;
    var ti = this.id;

    alert ("1: " + eti + "   2: " + eci + "   3: " + ti);
}

These 3 events, alerts the same value and I thought it also plays the same role but not in this link I found in SO: jquery function(event) event.target.id is blank when clicking linked text.

Now my question is:

1.) What is the difference between using: event.target.id, event.currentTarget.id and this.id?

2.) When should I use event.target.id, event.currentTarget.id and this.id?

3.) And which works better among these three?

Does anybody have an idea and explanation why?

like image 221
Makudex Avatar asked Sep 08 '15 11:09

Makudex


People also ask

What is the difference between event target and event currentTarget?

target is the element that triggered the event (e.g., the user clicked on) currenttarget is the element that the event listener is attached to.

What is the difference between this and event in JavaScript?

Answer. this : this will refer to the context in which the function is called if we use an arrow function, or this will refer to the context in which this is called if we are using a function expression. If we use a function expression we can use this instead of event.

What is currentTarget?

Definition and Usage The currentTarget property always refers to the element whose event listener triggered the event, opposed to the target property, which returns the element that triggered the event.

Can I use event currentTarget?

currentTarget is only available while the event is being handled. If you console. log() the event object, storing it in a variable, and then look for the currentTarget key in the console, its value will be null . Instead, you should either use console.


1 Answers

Try this example

<div id="maindiv" onclick="callback(event, this);">
  <span id="span" onclick="callback(event, this);"> SPan</span>
  <p id="p" onclick="callback(event, this);">This is p </p>
</div>

function callback(e, thisObj) {
       console.log('this = ', thisObj.id);
       console.log('target = ', e.target.id);
       console.log('currentTarget = ', e.currentTarget.id);
    }

event.target is what dispatches the event. ex: if you click on p event.target will be p but event.currentTarget will be p when callback of p will be called event.currentTarget will be maindiv when callback will be called cause of event bubbling.

`this` refers to `event.currentTarget`

See this one for details
https://developer.mozilla.org/en-US/docs/Web/API/Event/Comparison_of_Event_Targets

Here is a same question i think see this one
Difference between e.target and e.currentTarget

like image 54
intekhab Avatar answered Oct 26 '22 10:10

intekhab