2

I pretty new to Jquery/JavaScript and was wondering if someone could please explain exactly what (e) is doing? Is this similar to the "this" object?

My Jquery:

$(document).ready(function() {
    $('#testDiv').on('click', function(e) {
    var $clicked = $(e.target);
    $clicked.css('background', 'red');
  });
});
colxi
  • 7,640
  • 2
  • 45
  • 43
Kasador
  • 405
  • 7
  • 18

1 Answers1

1

The e parameter contains the Event Object, passed to your event Handler function. Inside the event object you'll find relevant data relative to the triggered event.

From MDN Web docs:

DOM Events are sent to notify code of interesting things that have taken place. Each event is represented by an object which is based on the Event interface, and may have additional custom fields and/or functions used to get additional information about what happened. Events can represent everything from basic user interactions to automated notifications of things happening in the rendering model.

If we are talking about a click event, for exampke, e.target would be a reference to the DOM Element wich received the click. Wich corresponds in this case with the value of this, inside the handler scope


You can find more information about Events here:

https://developer.mozilla.org/en-US/docs/Web/Events

colxi
  • 7,640
  • 2
  • 45
  • 43
  • So, it doesn't act as $(this). Because we specified which id we target. Which is in this case #testDiv. Is this then in a way like "return"? – Kasador Mar 28 '18 at 02:08
  • @CazadorShaw in this case it is the same of `this`, check here: https://jsfiddle.net/npuo9ouv/ – Gerardo Furtado Mar 28 '18 at 02:13
  • I think I'm understanding this a little better. it's just hard to wrap my head around this as a first time programmer. Thanks! – Kasador Mar 28 '18 at 02:19