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 trialcodekudos
15,271 PointsjQuery $(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
46,160 PointsYour 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
7,967 Pointsyou will need to use function (e) {} instead of (e) => {}
Jeff Sanders
Courses Plus Student 9,503 PointsFrom 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
14,768 PointsI had the same problem, this was helpfull, tks!
Tai Man Chan
Courses Plus Student 135 Pointsbecoz you're using anonymous function