1

Possible Duplicate:

In Javascript/jQuery what does (e) mean?

jQuery's .click - pass parameters to user function

Given this code:

function myFunc(e) {
    e.preventDefault();
}

var x = 99;
$('#myId').bind('click', myFunc);

Can someone tell me what is the e parameter. Also how could I pass the x parameter to myFunc ?

Community
  • 1
  • 1
Alan2
  • 23,493
  • 79
  • 256
  • 450
  • Have a look at [the docs](http://api.jquery.com/bind/) – Bergi Sep 26 '12 at 15:51
  • As there is no mention of data (x) in the linked "duplicate", I don't think this is a proper duplicate. – Denys Séguret Sep 26 '12 at 15:55
  • Given that specific code, you would have no reason to pass `x`. You'd just have `myFunc` do a direct reference. Of course your actual code may be different. – I Hate Lazy Sep 26 '12 at 16:02
  • 1
    @dystroy This question is a duplicate of http://stackoverflow.com/questions/10323392/in-javascript-jquery-what-does-e-mean and http://stackoverflow.com/questions/3273350/jquery-click-pass-parameters-to-user-function – Selvakumar Arumugam Sep 26 '12 at 16:04

1 Answers1

7

This is the click event, wrapped as a jquery event.

If you want to pass x, you may do this :

$('#myId').bind('click', {x:x}, myFunc);

Your function will find x in e.data.x.

If you need the native event (i.e. not the jQuery one), use e.originalEvent but many fields of the event are accessible through e.

Reference

Denys Séguret
  • 372,613
  • 87
  • 782
  • 758
  • thanks. glad to see your answer before the question was closed. – Alan2 Sep 26 '12 at 15:54
  • The question wasn't clear on this point, but it should be noted that the handler won't get `x` via event data. It'll get `99` with every invocation irrespective of the current value of `x`. To provide the current value, OP would need to wrap `myFunc` in another function that has direct access, and pass along the current value. All this of course assumes that `myFunc` is out of scope of `x`. If not, then obviously `myFunc` should just reference it directly. – I Hate Lazy Sep 26 '12 at 16:00