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 trialJeffrey Vierra
25,404 PointsConverting to an arrow function.
Hi,
I'm trying to convert this
$('li').on('click', function() {
$(this).text("Clicked!");
});
To an arrow funcion. I can't seem to figure it out.
The closest i've got is :
$('li').on('click',() => {
$(this).text("Clicked!");
});
Which is probably allot further than I think..
Any assistance would be greatly appreciated.
Thanks
2 Answers
Steven Parker
231,198 PointsActually the problem isn't the format conversion (which in itself is good), but the fact that your function uses "this" which is not defined in an arrow function like it is in a conventional one. Using "this" is one of several cases where arrow functions cannot be used.
Now what would work is if you also modify the function to accept and use the event object argument:
$('li').on('click', e => {
$(e.target).text("Clicked!");
});
For a detailed explanation of the differences between arrow functions and conventional functions, see this MDN page on Arrow Functions.
Jeffrey Vierra
25,404 PointsThank you Steven. You saved me hours of frustration :)