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 jQuery Basics Understanding jQuery Events and DOM Traversal What is Traversal?

codekudos
codekudos
15,265 Points

jQuery $(this) selector not working.

$('.spoiler').on('click', 'button', (evt) => {
     //show the spoiler text 
    $(this).prev().show();
    //hide the "reveal" 
    $(this).hide();

});

I tried using various browsers. I am able to use $(evt.target) with no issues

4 Answers

Balazs Peak
Balazs Peak
46,089 Points

Your code looks fine, though. I'd suggest to keep progressing with the lectures. You will know much more about the "this" keyword later on.

Andrew Karas
Andrew Karas
7,967 Points

you will need to use function (e) {} instead of (e) => {}

From MDN: "An arrow function expression has a shorter syntax than a function expression and does not have its own this, arguments, super, or new.target."

I also highly recommend reading this article: http://2ality.com/2012/04/arrow-functions.html and this https://dmitripavlutin.com/when-not-to-use-arrow-functions-in-javascript/

The "this" that you are selecting in your arrow function expression is actually 'Window'.

Bibiana Balbuena Barbosa
Bibiana Balbuena Barbosa
14,768 Points

I had the same problem, this was helpfull, tks!

becoz you're using anonymous function