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 One Solution

Piotr Manczak
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Piotr Manczak
Front End Web Development Techdegree Graduate 29,277 Points

Event object not available.

Have you noticed that event object is not available if the function is declated outside of the event listener? Hence e.target cannot be used as e does not exist. e.preventDefault() is recognised by browser only if validate() is declared within form event listener. Is my understanding correct?

1 Answer

Steven Parker
Steven Parker
231,198 Points

The event object should always be available to the handler if it is defined as the handler's argument. For example:

function myHandler(e) {              // define handler outside of listener
  e.target.textContent = "👻 Boo!";
}

myButton.addEventListener("click", myHandler);