How does the jQuery tag $(this)
exactly work? I know how to use it, but how does jQuery know which element is 'active'? And what is the original Javascript tag for getting the current item, or is it jQuery only?
How does the jQuery tag $(this)
exactly work? I know how to use it, but how does jQuery know which element is 'active'? And what is the original Javascript tag for getting the current item, or is it jQuery only?
- 7 @qor You linked to the current question, so yes – Michael Mrozek Commented Jul 29, 2010 at 14:12
- Nice to see SO protects closing a question because of being a duplicate of itself though - some sites wouldn't ;) – Konerak Commented Jul 29, 2010 at 14:16
- possible duplicate of Why do I have to use $(this) – jAndy Commented Jul 29, 2010 at 14:20
- 1 @Konerak IIRC you can still close two questions as duplicates of each other though – Michael Mrozek Commented Jul 29, 2010 at 14:21
4 Answers
Reset to default 7The this
is a simple javascript (DOM) object,
$(this)
will turn the object into a jQuery object.
jQuery doesn't need to 'know' what this
is, it doesn't treat this in a special way, no other than myHeaderDiv
in
var myHeaderDiv = document.getElementById('header');
$myHeaderDiv = $(myheaderDiv); //just a variable transformed into jQuery object, as with this.
this
is context-dependent in jQuery (and JavaScript in general). It usually represents the current DOM element in a event handler, but is not a jQuery object.
$(this)
is a jQuery object containing the current DOM element.
The expression $(this)
is just a regular Javascript function call, equivalent to jQuery(this)
. The this
value is defined by Javascript itself, and is not a jQuery invention.
Perhaps you should read about scope in JavaScript http://www.digital-web./articles/scope_in_javascript/