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 Events with jQuery

Paulo Portella
PLUS
Paulo Portella
Courses Plus Student 1,992 Points

JQuery Basics > Events with JQuery Challenge Task. What am I doing wrong?

Hi there,

I am stuck on Task Two, that's my code:

$('.student-list').on('click', function (event){ $('.student-list').hide(); });

That is the error message: Within the anonymous function passed to the "on" method call, did you call jQuery with the argument "event.target"?

I have tried different methods without success.

Thank you for the help, Paulo

3 Answers

Hi Paulo, your code is good. All you need do is use jQuery to select the 'event.target' and not the element with a class of 'student-list' like you did.

$('.student-list').on('click', function (event){
// you're meant to select the event.target and not the class of 'student-list'
 $('.student-list').hide(); 
});

You already attached the 'on' event handler to the 'student-list' class you selected with jQuery, so instead of selecting it again with jQuery........you're to select the event object (i.e. event.target) which uses event bubbling to handle the same events on its children.

$('.student-list').on('click', function (event){
// selecting the event.target with jQuery
 event.target.hide(); 
});
Paulo Portella
PLUS
Paulo Portella
Courses Plus Student 1,992 Points

Hi Osara, Thanks for your answer but I am still getting an error: Bummer! Unexpected MemberExpression.object type: MemberExpression

$('.student-list').on('click', function (event){
  event.target.hide();
});

Thank you

Ooops sorry I made a mistake, I was meant to wrap the event.target with $() to turn it to a jQuery object. It should be:

$('.student-list').on('click', function (event){
// selecting the event.target with jQuery
 $(event.target).hide(); 
});

It's pretty strange that I can have:

$('event.target').hide();

within Visual Studio Code (actual text editor) and it works just fine, yet I have to leave off the single quotation marks to pass the challenge.