Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

this vs $(this) [duplicate]

Tags:

Possible Duplicate:
jQuery $(this) vs this

I'm new to this and trying to get my concept right. There has been many instances of the use of "this" and "$(this)". Can someone please explain the difference and in what condition that we use the two different "this"?

like image 440
user864600 Avatar asked Sep 20 '11 02:09

user864600


People also ask

What is the difference between $( this and this?

this and $(this) refers to the same element. The only difference is the way they are used. 'this' is used in traditional sense, when 'this' is wrapped in $() then it becomes a jQuery object and you are able to use the power of jQuery.

What is $( this in Javascript?

$(this) is a jQuery object and this is a pure DOM Element object. See this example: $(".test"). click(function(){ alert($(this). text()); //and alert(this. text()); // error no method });

What does $() mean in jQuery?

In jQuery, the $ sign is just an alias to jQuery() , then an alias for a function. This page reports: Basic syntax is: $(selector).action() A dollar sign to define jQuery.


1 Answers

In jQuery functions, this most often refers to the actual DOM element you're dealing with, whereas $(this) returns a jQuery object that wraps the element.

In JavaScript, this always refers to the current scope. Many of jQuery's functions will set that scope to be the element you're working with.

For instance

$("#someElement").click(function() {     this;    // the element itself     $(this); // a jQuery wrapper-object around the element }); 

The point is, that the jQuery object has all the jQuery functions (like .detatch() or .prependTo() etc.), while the DOM element is what the browser provides. In the example above, the element would be exactly the same as what you'd get, if you called document.getElementById("someElement")

like image 163
Flambino Avatar answered Sep 22 '22 15:09

Flambino