0

I am new to jQuery and trying to find out what is the difference between following two syntax.

1. $( "#target" ).keyup(**function( event )** {
     alert("Event argument");
   }

2. $( "#target" ).keyup(**function(  )** {
     alert(" No Event argument");
   }

Html :

input id="target" type="text"

Does it make any difference in adding "event" argument in .keyup() definition ??

2
  • no difference at all.. you pass event argument if you need something to do with an event.. like preventing Default behaviour of an event, for getting a keycode Commented Jan 21, 2014 at 6:22
  • If you don't declare a parameter, then when the function is called the argument will be "lost" (or rather, not directly accessible via a variable). However, the function itself will be called in the exact same manner - so if you don't need the parameter then it's perfectly fine to omit it. Commented Jan 21, 2014 at 6:31

2 Answers 2

1

The parameters are passed to the function regardless. By putting them in the function signature, you're essentially creating local variables for each argument. For example, these are essentially the same thing.

$("#target").keyup(function() { 
  var event = arguments[0];
}

$("#target").keyup(function(event) {
}

If you don't intend on using the event variable, then not declaring it does no harm.

Sign up to request clarification or add additional context in comments.

Comments

1

It doesn't make any difference unless you need the event for something. In the case of a keyup event, the event object will include the keyCode which is very useful.

Try this out:

$("#target").keyup(function(event) { 
  alert(event.keyCode); 
}

It's a lot easier to work with console.log rather than alert. You might want to change to that so that you don't have to deal with popups all the time. View the log in the dev tools (probably f12 key).

This might help you understand what is going on better:

function foo(yourFunction) {
  var eventObject = {keyCode: 10};
  yourFunction(eventObject);
}

foo(function(event) {
  console.log(event);
});

In this example, function foo is called and a function is passed into it. foo then calls the passed in function and passes a value into that function. In order for that function to use it, the function has to give it a name. Look at this:

foo(function() { //now the function doesn't accept any arguments/parameters
  console.log(event); // what is "event" ???
});

The arguments passed into a function can be accessed with the variable arguments. To keep with my previous examples:

function foo() { //argument name removed
  var eventObject = {keyCode: 10};
  yourFunction(eventObject); //this is now undefined

  console.log(arguments); 
  //this will be an array
  //the first value of the array will be the function that was passed in
}

foo(function() { //argument named remove
  console.log(event); //this is also undefined
  console.log(arguments);
  //the first value of "arguments" will be the eventObject that was passed in
});

Whether you write the function to accept arguments or not, arguments can still be passed to it and are available with the arguments variable, but if you need them, you should give them a local variable name.

One last thing. It doesn't matter what you name an argument. You're just giving it whatever local name you want.

$("#target").keyup(function(e) {
  alert(e);
}
$("#target").keyup(function(evt) {
  alert(evt);
}
$("#target").keyup(function(event) {
  alert(event);
}
$("#target").keyup(function(cookieMonster) {
  alert(cookieMonster);
}

All of those do exactly the same thing. The variable refers to the same event object, since that is what is passed in.

3 Comments

will i be able to access event.which in second option ?
@user2953551 no, only if you pass it in.
$("#target").keyup(function() { console.log(arguments); } and you will find what's the arguments for the callback function.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.