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

codekudos
codekudos
15,267 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,160 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