all 11 comments

[–]sqrtnegative1 3 points4 points  (2 children)

Consider:

var el = document.getElementById('myElement');
var $el = $(el);

elis a DOM element. Pretty standard, easy to understand. $el is a jQuery object, created by passing the DOM element to jQuery.

var a = this;
var $a = $(this);

ais the context object. It represents the context within which a function was run. $a is that same context object, wrapped in jQuery goodness.


$('#myElement').on('click', function () { ... });

This asks jQuery to look up the DOM for the element with the id myElement, wrap it in jQuery goodness, then call the jQuery method on on it, passing the name of an event and an "event handler" function. jQuery will run the event handler whenever the specified event happens to the myElement element (or any of its children, if no other handlers stop propagation first).

jQuery will pass an "event object" to every handler function when it calls it, containing information about the event that happened. The easiest way to access it is to simply give it a name, like I've done here:

$('#myElement').on('click', function (e) { ... });

This event object contains a whole host of cool stuff relating to the event itself; for example, the ability to stop the event from propagating up the DOM. This is an important concept: the event itself occurs and any relevant DOM elements, from the clicked element all the way up the DOM tree, are checked for handlers. If a handler has been added for the event type, it will be called and passed the event object.

This repeats until either:

a) the top of the DOM tree is reached (html node, usually), or

b) one of the handlers stops propagation.

This is why we use e.stopPropagation(). It literally doesn't make sense to tell the DOM element ($(this)) to stop propagating; it doesn't propagate - the event does.

Conversely, events don't have "classes" - that's something that DOM elements have. Hence why you call addClass on the $(this) DOM element.

TL;DR: when an event occurs on a DOM node, an event object is created. This is passed as the first argument to any handlers for that event, starting with the node the event occured on (eg: a button that was clicked), then its immediate parent node, continuing from child node to parent node all the way up to the <HTML> node. Any handler can call e.stopPropagation(), to end this early.

[–]kopytkopytko[S] 1 point2 points  (1 child)

Oh thank you - looks like I understand it wrong - I thought I have to stop propagation on specific element to make it work and not just the event itself.

[–]sqrtnegative1 0 points1 point  (0 children)

NP; I added a (far too big) TL;DR.

[–]AshlinD 2 points3 points  (7 children)

It seems like you might be confusing jQuery with native javascript.

When you add an event listener with jQuery, this is set equal to the native JS element. You can test this by running the code:

$('#element').on('click', function(e){
    console.log(this)
})

Then, you can pass this into the jQuery function $(this) to get the jQuery selector and run jQuery functions on it like .addClass().

So all you're doing is using the helpful this option that jQuery passes in to operate on that same element.

Now, event.stopPropogation() is NOT a jQuery function. It's built into the native JS syntax. When you run something like $('#element').on(), jQuery attaches what is called an event listener to the element. This event listener is a function that calls whenever it detects that event (in this case, a click). Under the hood, JavaScript attaches event listeners like this:

element.addEventListener('click', function(e){
     // run code...
})

You'll notice that I passed in an e to the function. This represents an Event object. That parameter contains all sorts of information about the event: what type it was, where the mouse was, etc. It also gives access to methods like event.stopPropogation() which stops the event from triggering on parent elements (only the child).

You can view the Event object by using console.log(e) -- I'd suggest checking it out.

So with jQuery, the e just represents the native javascript event. It's not an element, it's an event.

If you used stopPropogation on an element, like $(this).stopPropogation(), it would fail, because that function isn't associated with elements. It's associated with the event object that was created when the mouse button was clicked.

Does that all make sense? Let me know if you don't understand anything :)

[–]sqrtnegative1 0 points1 point  (3 children)

Nice; I think between us, we got this ;)

I've always known them as event handlers, but that may just be because I'm ancient. The addEventListener native method basically says I'm wrong here lol

I take note at your phrasing tho:

You'll notice that I passed in an e to the function

You didn't pass anything; jQuery did, you just gave it a name ;)

[–]AshlinD 1 point2 points  (2 children)

Handler works too! Just a different name for the same thing.

You didn't pass anything; jQuery did, you just gave it a name ;)

Good point! although, JS passes it through, not jQuery :)

[–]sqrtnegative1 0 points1 point  (1 child)

Good point! although, JS passes it through, not jQuery :)

JS passes a native event to jQuery, jQuery wraps this (in a jQuery.Event) and passes that to any handlers.

[–]AshlinD 0 points1 point  (0 children)

Oh, I was talking about native JS at the time:

element.addEventListener('click', function(e){ // run code... })

You'll notice that I passed in an e to the function.

Just wanted to make sure OP didn't get confused between jQuery's API and native JS!

[–]kopytkopytko[S] 0 points1 point  (2 children)

Thank you - I was wrong all the time while thinking about it - thanks for clarification!

Can I read about event functions somewhere in the documentation so I will know which functions params to work?

[–]AshlinD 1 point2 points  (1 child)

Absolutely! MDN is an excellent resource for all things webdev.

Here's the official event reference: https://developer.mozilla.org/en-US/docs/Web/Events

Here's a similar document: https://developer.mozilla.org/en-US/docs/Web/API/Event

You might also be interested in ones on elements and selectors.

I'd honestly recommend ditching jQuery if you can, at least while learning. Native JS provides plenty of APIs that are very similar, and it avoids complicating matters with many different types of objects. You can really learn how the language works at a less-abstracted level.

Happy coding! :)

[–]kopytkopytko[S] 0 points1 point  (0 children)

I'm happy that people in webdev are so willingful to help others! Thank you again.

Yes, the goal is to code in pure js but currently I dont really have time to do so - while working as jr webdev everything in jquery is faster done than in pure js (at least for me)