Reputation: 13
I know that E is short for 'event,' but it's still not clear to me why I need to use it? I tried passing other letters through the function and it didn't seem to work. Are there other situations where I need to pass a specific letter into a function?
$("#myThing").click(function (e) {
var hit = $(e.target);
hit.css('color','blue');
});
This does the same thing:
$("#myThing").click(function () {
$(this).css('color','blue');
});
When should I use $(this) versus 'e'?
Thank you!
Upvotes: 1
Views: 237
Reputation: 123377
there's a small difference between the two snippets: in the first one, the element hit
could not be the #myThing
element itself.
Let say that #myThing
is a <p>
and you have clicked on a link inside it: then when the event will reach #myThing
, the element referred by e.target
is the link and not the paragraph
On the contrary, in the second example the element subject to a color change will always be #myThing
So, if you pass the event along with the handler it's because you have a convenience on using that event. When you pass it, you can name it e
or evt
or also foobar
: since you're using jQuery the variable name you choose is not relevant (but for the sake of simplicity e
is short and widely used).
The most common reasons to pass the event usually are
e.keycode
on keyup/down
event);Upvotes: 1
Reputation: 106385
See, in this case event.target
and this
are referring to the same element. But when you use jQuery event delegation, they may not match:
$('body').on('click', 'tr', function(event) { ... });
Here event.target
will refer to the element at which the event was triggered (it may be 'td' element or its children, for example), but this
will refer to the tr
element. Sometimes it's quite handy to have both of them. )
Besides, yes, event object has some other uses: with it you can stop propagation of the event, etc.
Upvotes: 1
Reputation: 5515
Events can carry data with them (eg a Dropped file). If you need to refer to the data that belongs to the event, use "e". If you only need to refer to the element that the event was triggered from - which is what 'this' refers to in an event callback - use 'this'.
Upvotes: 1
Reputation: 75317
The name e
is meaningless; you can call it whatever you want (obviously so long as you update the references to e
as well).
The code you posted will usually do the same thing; but see the following code:
<div><span>Hi</span></div>
JS:
$('div').on('click', function (e) {
alert(this === e.target);
});
(Run in here: http://jsfiddle.net/Dxbwm/)
You'll see it's false; thats because this
is the element the event is handled on, but e.target
is the element the event originated from (which in the example above, is the span). If you didn't know events worked like this before, it's called event bubbling and you can read more about it heremy blog.
Upvotes: 2
Reputation: 59333
You don't need to pass a specific letter through the function.
$("#myThing").click(function (e) {
var hit = $(e.target);
hit.css('color','blue');
});
is identical to
$("#myThing").click(function (foo) {
var hit = $(foo.target);
hit.css('color','blue');
});
etc.
The difference between using $(e.target)
and $(this)
can be quite more significant though, since this
will always be the element on which the event is tied, while e.target
will be the specific element that was clicked, even though it's located inside the this
element
Hope that made sense
Upvotes: 4