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

Miles Falk
Miles Falk
6,566 Points

Arrow function syntax not working on this project

Dear Treehouse Community,

I am attempting to use arrow function syntax on this project but when I veer away from the regular anonymous functions I seem to return the window object and not the button I hoped for. Maybe this is just my misunderstanding of how 'this' works or my misinterpretation of the current lecture. Thanks in advance!

My code with a regular anonymous function:

$('button').on('click', function () { $('button').removeClass('selected'); $(this).addClass('selected'); });

My code with arrow function syntax :

$('button').on('click', () => { $('button').removeClass('selected'); $(this).addClass('selected'); });

1 Answer