0

I have seen this being used through multiple languages so it seems to be just a coding question in general. On an answer here at stackoverflow, it seems as though he used a variable as a parameter, but e has not been defined before this function.

$(document).keypress(function(e) {
    if(e.which == 13) {
        alert('You pressed enter!');
    }
});

What is happening with "e" in situations like this?

Community
  • 1
  • 1
user1971
  • 698
  • 2
  • 6
  • 18
  • What exactly do you mean? e is the parameter of the function and the code that calls it will pass an argument. – Felix Kling Feb 21 '14 at 02:13
  • Seeing as how this is javascript, the function parameter is actually defined `e`, it's just not strongly typed – Martin J. Feb 21 '14 at 02:23
  • I'd like to know how it knows what information e is giving the function. Would it be the context of how it is used? .which listens for events, so would that mean that e is used to listen for all key and mouse events? – user1971 Feb 21 '14 at 18:55

2 Answers2

0

"e" is the event handler and holds further information about the specific event that you trapped.

JBES
  • 1,512
  • 11
  • 18
0

function(e) actually is a callback. e has been initialised somewhere in the jQuery library and inserted into the anonymous function's parameter.

Kyle Emmanuel
  • 2,193
  • 1
  • 15
  • 22