Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jquery - When to use "this" and when to use "$(this)"? [duplicate]

Tags:

jquery

Possible Duplicate:
jQuery $(this) vs this

What is the difference between "this" and "$(this)"?

How do I know which one to use?

Related, I think:

With each, you have the optional parameters. How is the "i" different than "this" (or "$(this)")?

$('img').each(function(i) { ....code }

vs.

$('img').each(function() { ....code }
like image 883
Buttle Butkus Avatar asked Dec 12 '11 03:12

Buttle Butkus


1 Answers

the this object doesn't change. It is the owner of the function. It is, in most cases like this, simply a node and you can reference all of its properties like this.className. (think of it as you would a node or whatnot that you get with document.getElementById). It is just the "owner" of the function.

Therefore, you are just passing the this object to jQuery's $().

Conclusion: If you want to use jQuery functions for the current node, use $(this). But if you want to access the objects own properties (e.g. .name, className, .id), use simply this.

like image 168
Joseph Marikle Avatar answered Nov 16 '22 02:11

Joseph Marikle