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 Spoiler Revealer Perform: Part 3

$(this).remove(); I didnt understand

I didnt understand why we use "this" in $(this).remove(); what does it exactly do? and when should we use it?

2 Answers

Think of this as a pronoun meaning "me". Then $(this) means "select me". Here "me" is the button that was clicked in line 9 of app.js. In short, this is a way to refer to the current button.

It might help, with that in mind, to re-read what Andrew said at 2:11 in the video: Because you're clicking on a particular button, there's a special variable called this, which means this object that something's happening to. So, in this instance a object, a button, a particular button is being clicked. Not all buttons are being clicked. Just this particular one.

As Andrew also pointed out, if you had tried to select the button with $(button) and there were more than one button you would be selecting all of them, rather than just the current button.

jason chan
jason chan
31,009 Points

This is hard concept to understand. basically this means this. When you click this button. You are refering to this button you clicked.

It's literally means to point at this something.