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

Myles Gleeson
Myles Gleeson
2,906 Points

Why can't you access event.target directly?

$('.spoiler').on('click', 'button', function(event){
  console.log(event.target);
  // Show the spoiler text
  $('.spoiler span').show();
  // Hide "reveal spoiler" button
  $(event.target).hide();

});

Why can't you hide the reveal spoiler button using simply event.target.hide() ?

Steven Parker
Steven Parker
231,275 Points

The "View Challenge" button seems to link to an unrelated part of the course. Can you provide a link to the part that relates to this question?

2 Answers

Justin Cantley
Justin Cantley
18,068 Points

.hide() is a method that can only be called on a jQuery object.
Therefore, the only way to call .hide() on event.target is to first convert event.target into a jQuery object.

andren
andren
28,558 Points

Justin Cantley's answer is right.

To reiterate hide is a method that belongs to jQuery, it is not a method built into JavaScript's HTMLElement object. That is why you cannot call it directly on event.target.

Steven Parker
Steven Parker
231,275 Points

The methods and properties available on HTML elements are different from those on jQuery objects.

Here's a "direct" way to conceal an HTML element:

event.target.style.display = "none";