Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript jQuery Basics (2014) Creating a Simple Drawing Application Perform: Part 1

Gavin Broekema
seal-mask
.a{fill-rule:evenodd;}techdegree
Gavin Broekema
Full Stack JavaScript Techdegree Student 22,443 Points

$(this)

Can someone give me some more insight as to when I should be using this vs. not? In previous lessons Andrew has exchanged the $(this) with variables, which has left me completely confused.

1 Answer

Rick Buffington
Rick Buffington
8,146 Points

The this object is a reference to the parent object in the current function/scope. For example:

$("#potato").click(function() {
  $(this).hide();
}

In the above example, this refers to the #potato item that jQuery selected. The reason for this is because when $(this) is being referenced, it is within the click function. You could have also written it as:

$("#potato").click(function() {
  $("#potato").hide();
}

The two are equivalents - however this may be more appropriate when dealing with multiple jQuery selectors.