views:

363

answers:

3

I'm novice with both JS and jQuery, and I'm a little bit confused about what situations would require you to pass event as an argument into the function, and what situations you would not need to.

For example:

      $(document).ready(function() {

        $('#foo').click(function() {
         // Do something
});

      });

versus

      $(document).ready(function() {

        $('#foo').click(function(event) {
         // Do something
});

      });
+1  A: 

You only need the event if you're going to use it in the body of the handler.

Nosredna
What's an example of a way I would 'use' the event in the handler?
Matthew
If you want to do something in the handler that requires knowing more information about the event, such as the location of the mouse. You might get some ideas if you put a breakpoint inside the handler and looked at what's available in the event object.
Nosredna
+3  A: 

The event argument has a few uses. You only need to specify it as an argument to your handler if you're actually going to make use of it -- JavaScript handles variable numbers of arguments without complaint.

The most common use you'll see is to prevent the default behavior of the action that triggered the event. So:

$('a.fake').click(function(e) {
    e.preventDefault();
    alert("This is a fake link!");
});

...would stop any links with the class fake from actually going to their href when clicked. Likewise, you can cancel form submissions with it, e.g. in validation methods. This is like return false, but rather more reliable.

jQuery's event object is actually a cross-browser version of the standard event argument provided in everything but IE. It's essentially a shortcut, that lets you use only one code path instead of having to check what browser you're using in every event handler.

(If you read non-jQuery code you'll see a lot of the following, which is done to work around IE's deficiency.

function(e) {
    e = e || window.event; // For IE

It's a pain, and libraries make it so much easier to deal with.)

There's a full accounting of its properties in the jQuery docs. Essentially, include it if you see anything you need there, and don't worry otherwise. I like to include it always, just so I never have to remember to add it in later if I decide that it's needed after all.

David
+1  A: 

Since you are using jQuery, you only put event as an argument if you need to use the event in the handler, such as if you need the key that was pressed on a keypress event.

In JS, without jQuery or Prototype etc., you need to pass the event as a parameter for standards compliant browsers like Firefox, but the event is not passed as an argument in IE. IE actually maintains a global variable window.event. So in your handler (sans library) you need to check if the argument is undefined; if so, grab the global variable.

function eventHandler(evt) {
  var theEvent = evt || window.event;
  //use the event
}

But a library like jQuery takes care of that for you.

I honestly don't recommend using a library until you have learned the language. But if this is for a job, the by all means use the library, but learn the details of JS on your own, so you can better appreciate it.

geowa4
I prefer much the opposite, actually. Learn a library first, which lets you avoid the language's little quirks (in JS's case, cross-browser issues). In time, once you gain familiarity with the subset of the language you're using, experiment with library-free work.
David
Personal preference I guess. But I think that I got good a lot faster dealing with the language's quirks.
geowa4