Monday, 9 September 2013

Passing "el" inside jQuery function?

Passing "el" inside jQuery function?

This is obviously very common, I cannot find a clear explanation of
passing "el" inside a function.
I just really want to fully wrap my head around it, I am getting much
better at jQuery it's exciting but I want to understand this more.
I toyed with using "e" or "event" inside an event function and understand
it to a degree.
EX:
jQuery('a').click(function(event) {
event.preventDefault();
$('<div/>').append('default ' + event.type + '
prevented').appendTo('#log2');
});
This returns this string of text "default click prevented" so I now
understand why you pass event it's so you can refer to the event inside.
right?
Well that makes sense, I still need more practice with that, but I am not
sure about "el" at all.
Ex: if I wrote a function like this.
var app = {
add: function (el) {
$('<div/>').append('default ' + el.type + '
prevented').appendTo('#log1');
}
}
jQuery('.add').click(function (e) {
e.preventDefault();
app.add(this);
});
Obviously thats wrong because "el" is not a "type" but I am trying to play
with it to wrap my head around it, is it to get the index of an object?
I may have better luck if I knew what this method is called because I am
having trouble finding information on this due to lack of terminology. I
know the first example is "passing an elements event" not so sure when its
like this, pretty sure its to refer to the object or something?
Here is a fiddle maybe you can modify it so I can study it jsfiddle

No comments:

Post a Comment