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

CSS jQuery Basics (2014) Creating a Password Confirmation Form Perform: Part 1

Events == Methods?

Just a quick question.

I've been calling the jQuery things that end with parentheses "methods". My question is, would calling those things "jQuery events" be synonymous with calling them "jQuery methods"

1 Answer

Juliana Arrighi
Juliana Arrighi
9,928 Points

Hi Alex,

You're right to use the term method for things like .click() and .keypress(). These particular methods bind events with an event handlers, and they're named for the events that they are attaching the handler to. When Andrew says he's looking for an "event" in the jQuery API documentation, he means he's looking for a method related an event we care about. So calling the things with parentheses "events" isn't exactly right, but you may hear people say that as verbal shorthand.

This jQuery article may help clear up the distinction.

But here's a quick, specific example based on the jQuery documentation for .keypress() to help clarify the difference between "events" and "methods":

A key press is an event that occurs when the user presses certain keys on their keyboard. Then, a keypress event object is passed to the element in the DOM that has focus when it is triggered. .keypress() is a method that allows you to specify the event handler for a "keypress" on a particular element. You call the .keypress() method on a selector for the element you care about.

Say you have some code like:

function keypressHandler (event) {
    console.log("a key was pressed");
}

$('#target').keypress(keypressHandler);

If the element in the DOM with id="target" has focus when the user presses a key, a keypress event object will be passed to that element. Since we specified with the .keypress() method that the keypressHandler is the event handler function for keypress events on that element, the keypressHandler function will then be executed and "a key was pressed" will be printed to the console.