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


Hazim Bora Alap
Hazim Bora Alap
15,001 Points

Arrow Function

not 100% related, but I'm using ES6 arrow function on this exercise and for some reason arrow function doesn't work on the second time

$('#add-pet').on("click", () => { const $petName = $('#pet-name'); const $petSpec = $('#pet-species'); const $petNote = $('#pet-notes');

const $newPet = $( '<section class="six columns"><div class="card"><p><strong> Name: </strong> ' + $petName.val() + '</p><p><strong>Species:</strong>'+ $petSpec.val() + '</p><p><strong>Notes:</strong>'+ $petNote.val() + '</p><span class="close">Ɨ</span></div></section>' );


$petName.val(""); $petSpec.val("Dog"); $petNote.val("");

$('.close').on('click', () => { $(this).parent().remove(); }); })

it works as

$('.close').on('click', function() { $(this).parent().remove(); });

is there a specific reason for this ?

1 Answer

Steven Parker
Steven Parker
230,230 Points

Arrow functions don't work exactly like conventional functions.

In this case, the important difference is that arrow functions do not set the value of "this". So if you want to use one as an event handler, be sure to pass and use the event object:

$('.close').on('click', e => { $(e.target).parent().remove(); });

Check the MDN page for more information on how arrow functions are different.