-1

I know that you can't pass a parameter to the anonymous function on say, a click event:

$(.....).click(function(e,'parameter-that-can\'t-be-passed'){
})

But why? I don't understand how the event object is even passed to the function. How is a limitation imposed on the anonymous function by the event handler? It just seems kind of strange to me.

Update

For those of you who may stumble upon this question also wondering where the object comes from, it may be due to a lack of knowledge about function callbacks and I encourage you to see this question:

Getting a better understanding of callback functions in JavaScript

Community
  • 1
  • 1
Craig Lafferty
  • 771
  • 3
  • 10
  • 31
  • You forgot to show us your code which probably contains some mistake. – ThiefMaster Mar 14 '14 at 22:05
  • 1
    With that rep you are supposed to know how to ask and what to include to this kind of questions... Please show your code. – Teemu Mar 14 '14 at 22:06
  • Ok trolls, this was a question of curiosity not help. I changed the question entirely so you don't feel so insecure about not understanding it. Feel free to undo the downvote as you can now feel safe.:) – Craig Lafferty Mar 14 '14 at 22:24
  • possible duplicate of [Why do you have to pass the event object as a parameter?](http://stackoverflow.com/questions/10218722/why-do-you-have-to-pass-the-event-object-as-a-parameter) – T J Mar 14 '14 at 22:28
  • broken link @TilwinJoy – Craig Lafferty Mar 14 '14 at 22:31
  • @CraigPatrickLafferty doesn't seem broken to me. – T J Mar 14 '14 at 22:35
  • Nevermind, there was another link that you or someone else must have deleted that's no longer there. – Craig Lafferty Mar 14 '14 at 22:39
  • 1
    The "specific details" of [`click`](http://james.padolsey.com/jquery/#v=2.0.3&fn=jQuery.fn.click). – Teemu Mar 14 '14 at 22:44

1 Answers1

1

JQuery allows a couple of functions that allows you to pass additional data to a click event. Heres an example

HTML :

<button id="button1">HI</button>
<button id="button2">HI</button>

JS :

var custom = "Hello world";

$('#button1').on('click', {txt:custom}, function (e) {

    this.innerHTML = e.data.txt; // button 1 text changes to Hello world
});

var customfn = function (txt) {
    // the scope of the txt argument will be
    // available in the returning function 
    return function () {
        this.innerHTML = txt; // button 2 text changes to BYE
    }
}

$('#button2').on('click', {txt:custom}, customfn("BYE"));

You should also take a look at jQuery.proxy. I haven't tested the code out but the basic idea is there

user1318677
  • 116
  • 4