Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does $(this) work in jQuery

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?

like image 584
Tim Avatar asked Jul 29 '10 14:07

Tim


2 Answers

The 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.
like image 52
Konerak Avatar answered Sep 19 '22 06:09

Konerak


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.

like image 25
Powerlord Avatar answered Sep 20 '22 06:09

Powerlord