$ (this) vs this in jQuery

What is the difference between $(this)and thisin jQuery? Here are two different uses:

 $(document).ready(function() {
   $("#orderedlist").find("li").each(function(i) {
     $(this).append( " BAM! " + i );
   });
 });


 $(document).ready(function() {
   // use this to reset several forms at once
   $("#reset").click(function() {
     $("form").each(function() {
       this.reset();
     });
   });
 });
+5
source share
3 answers

The variable "this" refers (in cases like the event handlers you provided) to the DOM element. So $ (this) is a jQuery object containing only one DOM element.

You should use the usual "this" when you need to use your own DOM APIs, and $ (this) when you need jQuery help. Your second example is a good illustration; another may be when you just need the "id" or "name" of the element.

+10
source

, this dom, . $(this) jquery dom.

EDIT: , DOM jquery, , dom. this , document.getElementById(id)

+9

$(this)is a jquery object, and thisis a native dom object

+8
source

All Articles