Thursday, May 23, 2024
 Popular · Latest · Hot · Upcoming
142
rated 0 times [  143] [ 1]  / answers: 1 / hits: 45143  / 13 Years ago, tue, september 20, 2011, 12:00:00

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?


More From » jquery

 Answers
7

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)


[#90020] Saturday, September 17, 2011, 13 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
nikoguym

Total Points: 339
Total Questions: 106
Total Answers: 95

Location: Mali
Member since Sat, Feb 12, 2022
2 Years ago
nikoguym questions
Wed, Sep 22, 21, 00:00, 3 Years ago
Sun, Dec 13, 20, 00:00, 4 Years ago
Fri, Sep 18, 20, 00:00, 4 Years ago
;