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 (2014) Creating a Simple Drawing Application Perfect

Mark DeArmond
Mark DeArmond
15,233 Points

Event handler as function parameter specific to jQuery event method?

I might be calling these elements by the incorrect name so I'll try to represent my question in a visual way.

$( 'element' ).jQueryEventMethod( function( 'event handler as function parameter' ) {
} );

or

$( 'element' ).keydown( function( e ) {
   //function to execute               
} );

Andrew mentioned in another comment that, e is the event that is passed in from any event that's been triggered.

Now, if I were using the keydown() jQuery event method, would the "event handler as the function parameter" be passed in after a key has been pressed down or after ANY event that's been triggered?

1 Answer

Hey Mark,

First off, nice name! haha Now to get down to brass tax, the event object is passed in as soon as any event handler is fired off, provided that a name has been given inside the function call. In the event of key presses, you can get the which property of the event handler which returns the keycode of the key pressed. All you have to do is give this event handler a name and then you can use it i.e.

//event can be named anything you'd like provided it would be a valid variable name
$("input[type=text]").keydown(function(event) {
  //13 is the keycode for the Enter key
  if (event.which === 13) {
    alert("You pressed the Enter key!");
  }
});
Mark DeArmond
Mark DeArmond
15,233 Points

I appreciate the complement and the same goes to you as well. Haha. JavaScript can be fairly daunting and I'm having a little bit of trouble understanding some of the deeper aspects of it. Thanks for your help Marcus!

For me, the very best way I learned was to take pieces of code and make my own things with it. I say do as Fleetwood Mac would do and "Go Your Own Way". Haha :D Good luck, Mark!

Mark DeArmond
Mark DeArmond
15,233 Points

Haha. Such an eloquent way of putting it. I try to do that but, I get hung up on a lot of things that I don't understand. Definitely love using Treehouse though! It, along with people such as yourself, have been such an amazing help. Thanks Marcus!

You're very welcome! If you want some extra help, I have to highly suggest going to http://www.codeacademy.com and taking their basic JavaScript course. It really gets you hands on with JavaScript, and you'll leave the course with a MUCH better understanding of JS, which will be a big help on Treehouse.