Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

JavaScript

Tushar Singh
PLUS
Tushar Singh
Courses Plus Student 8,692 Points

jQuery (this)

I am currently on the jquery Basics course, the problem is :

I can easily solve all the code challenges and stuff but I have a feeling that even though I am completing those challenges, I am missing the logic behind the code.

And probably because mostly code challenges are plain stupid, anyone can solve them.

To get the better understanding of jquery, what should I do??

And when we write 'this', what does it mean?? I cant understand the keyword 'this ' refers to what exactly ?

And attr(), it is used to add a certain attribute , it may be a class, id or anything.. right??

Yongshuo Wang
Yongshuo Wang
5,500 Points

The course only giving you a basic understanding. To get yourself fully understand, you need to do more practise and read some other docs.

Such as : http://www.w3schools.com/jquery/default.asp

Hope this can help you.

1 Answer

Kevin Korte
Kevin Korte
28,135 Points

The best way I feel to understand anything it so just keep using it, and avoiding copy and pasting code that "works" but you don't understand how. Sometimes when I get stuck, and I find code that works, I will copy it, but than spend the time to go back through and figure out why it works using the documentation.

I find it helpful to write my own step by step in the comments of the code. One it makes sure I understand how its working, two when I come back to that code 3 months later, I can more easily remember what the code does, and how since the comments are my own.

The keyword this referes to whatever the code that is running is currently bound to. Whatever object we are working on, this is going to refer it it. An example might be say you have a photo gallery, and when a user clicks a photo, it flips over and on the back is some information. In your code, you could use this, and it would refer to the the particular image that was clicked.

Yes, attr is flexible enough to set, check, or manipulate attributes on an element. You can do all kinds of things, and add logic to how an attribute is set, or check if one is set. Many times, there are more then one way make something work, and sometimes there is a better way. In the event of setting a class, jquery also has a. .addClass() and .removeClass() methods.