Peter Berg
Peter Berg

Reputation: 6206

Calling jQuery $ function on 'this'

In the following code, why is it necessary to surround 'this' with the $ function?

var x = $('div');
x.click(function(){
    $(this).hide();
});

doesn't 'this' just refer to the expression x which is itself a jQuery object?

Upvotes: 0

Views: 55

Answers (3)

fromBrazL
fromBrazL

Reputation: 22

this reffers div ... thats mean that your div will hide when you click in it.

this always reffers what its in context.

Upvotes: 0

Denys Séguret
Denys Séguret

Reputation: 382160

No : this is the unwrapped DOM element, as specified in the documentation :

The handler parameter takes a callback function, as shown above. Within the handler, the keyword this refers to the DOM element to which the handler is bound. To make use of the element in jQuery, it can be passed to the normal $() function.

Upvotes: 1

bfavaretto
bfavaretto

Reputation: 71918

The value of this will be the DOM node representing the clicked element. $(this) will be a jQuery object wrapping that DOM node, and providing extra functions like .show(), .append(), etc.

Upvotes: 5

Related Questions