How to pass a parameter for a function parameter in a function

Discussion in 'Javascript' started by AzamSharp, Jul 4, 2008.

  1. AzamSharp

    AzamSharp Guest

    function foo(msg)
    {
    alert(msg);
    }



    var insertForm = function()
    {
    this.button = $('add_customer');

    this.button.observe('click', foo('hello world'));
    }

    The above line ' this.button.observe('click', foo('hello world'));'
    does not really work. It will work if I don't pass any parameters.
    AzamSharp, Jul 4, 2008
    #1
    1. Advertising

  2. AzamSharp

    SAM Guest

    AzamSharp a écrit :
    > function foo(msg)
    > {
    > alert(msg);
    > }
    >
    >
    >
    > var insertForm = function()
    > {
    > this.button = $('add_customer');
    > this.button.observe('click', foo('hello world'));
    > }
    >
    > The above line ' this.button.observe('click', foo('hello world'));'
    > does not really work. It will work if I don't pass any parameters.


    perhaps :

    var insertForm = function()
    {
    this.button = $('add_customer');
    this.button.observe('click', function(){foo('hello world'))};
    }

    --
    sm
    SAM, Jul 4, 2008
    #2
    1. Advertising

  3. AzamSharp

    SAM Guest

    SAM a écrit :
    >
    > perhaps :
    >
    > var insertForm = function()
    > {
    > this.button = $('add_customer');
    > this.button.observe('click', function(){foo('hello world'))};
    > }
    >



    better :

    var insertForm = function()
    {
    this.button = $('add_customer');
    this.button.observe('click', function(){ foo('hello world') });
    }


    --
    sm
    SAM, Jul 5, 2008
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Spartanicus
    Replies:
    4
    Views:
    1,366
  2. Martoon
    Replies:
    9
    Views:
    805
    Kai-Uwe Bux
    Apr 20, 2006
  3. Replies:
    1
    Views:
    530
    mlimber
    Jun 7, 2006
  4. Leo jay
    Replies:
    8
    Views:
    404
  5. soni2926
    Replies:
    4
    Views:
    108
    William James
    Nov 21, 2008
Loading...

Share This Page