Difference between $(this) and this in jquery
$(this) is the current object that was selected using a jQuery selector or event attached to the object.
so if you have $('#myelement').click(.....
then $(this)
referes to the element that was clicked on so that $(this).hide()
hides that element.
$(this) - represent current DOM element on which event this function is called
The this keyword - In JavaScript this always refers to the “owner” of the function we're executing, or rather, to the object that a function is a method of.
this
is the DOM object, whereas $(this)
is the jQuery wrapper around same.
When using this
, you can call DOM methods on it, but not jQuery methods. When using $(this)
, you can call jQuery methods on it, but not DOM methods.
In jQuery, this refers to the DOM object, and $(this
) refers to the same object but with jQuery methods added
you can't call this.each()
because each is not a DOM method, its a jquery method
you can call $(this).each()
because $(this)
returns a jquery object