how to chain event callbacks

Discussion in 'Javascript' started by fxn, Jun 27, 2007.

  1. fxn

    fxn Guest

    I want to add programmatically a function call to some onkeypress,
    taking into account that perhaps it already has something assigned.
    This addition is done on window load with the following code (it has
    some Prototype mixed in but it is minimal):

    function add_autofit() {
    $$('.autofit').each(function (e) {
    var atf = function (input) { autofit(input); };

    var current_onkeypress = Prototype.emptyFunction;
    if (e.onkeypress)
    current_onkeypress = e.onkeypress;

    e.onkeypress = function () { current_onkeypress(this);
    atf(this); };
    autofit(e);
    });
    }
    Event.observe(window, 'load', add_autofit);

    Looks like I don't understand the scope of this, because autofit
    alerts it has received the window object as input. I do not know how
    to get that right, would you help please?

    -- fxn

    PS: I am not using listeners because I need to invoke onchange
    programmatically afterwards.
    fxn, Jun 27, 2007
    #1
    1. Advertising

  2. fxn

    RobG Guest

    On Jun 27, 9:33 pm, fxn <> wrote:
    > I want to add programmatically a function call to some onkeypress,
    > taking into account that perhaps it already has something assigned.
    > This addition is done on window load with the following code (it has
    > some Prototype mixed in but it is minimal):


    Not "minimal" at all - it uses Prototype.js's Prototype object, $$()
    CSS selector and Event function objects and .each Array method
    extension. The scope issue you are having is common when attaching
    handlers and using the this keyword.

    You can get help specific to Prototype.js at the Ruby on Rails:
    Spinoffs group:

    <URL: http://groups.google.com.au/group/rubyonrails-spinoffs?lnk=li&hl=en
    >



    --
    Rob
    RobG, Jun 27, 2007
    #2
    1. Advertising

  3. fxn

    fxn Guest

    On Jun 27, 2:05 pm, RobG <> wrote:
    > On Jun 27, 9:33 pm, fxn <> wrote:
    >
    > > I want to add programmatically a function call to some onkeypress,
    > > taking into account that perhaps it already has something assigned.
    > > This addition is done on window load with the following code (it has
    > > some Prototype mixed in but it is minimal):

    >
    > Not "minimal" at all - it uses Prototype.js's Prototype object, $$()
    > CSS selector and Event function objects and .each Array method
    > extension. The scope issue you are having is common when attaching
    > handlers and using the this keyword.
    >
    > You can get help specific to Prototype.js at the Ruby on Rails:
    > Spinoffs group:


    I know that group, but my guess is that this comes from a
    misunderstunding about:

    * The scope of the this keyword in the plain JavaScript closures

    * How write the analogous of onchange="foo(this)" in plain JavaScript

    I think that doesn't work because the non-Prototype code, which is the
    key of my question, has the concepts wrong. I could reproduce this
    with no Prototype code. Just attaching that to window.onload and
    iterating over elements of autofit class with regular calls.

    -- fxn
    fxn, Jun 27, 2007
    #3
  4. fxn

    fxn Guest

    On Jun 27, 2:39 pm, fxn <> wrote:
    > On Jun 27, 2:05 pm, RobG <> wrote:
    >
    > > On Jun 27, 9:33 pm, fxn <> wrote:

    >
    > > > I want to add programmatically a function call to some onkeypress,
    > > > taking into account that perhaps it already has something assigned.
    > > > This addition is done on window load with the following code (it has
    > > > some Prototype mixed in but it is minimal):

    >
    > > Not "minimal" at all - it uses Prototype.js's Prototype object, $$()
    > > CSS selector and Event function objects and .each Array method
    > > extension. The scope issue you are having is common when attaching
    > > handlers and using the this keyword.

    >
    > > You can get help specific to Prototype.js at the Ruby on Rails:
    > > Spinoffs group:

    >
    > I know that group, but my guess is that this comes from a
    > misunderstunding about:
    >
    > * The scope of the this keyword in the plain JavaScript closures
    >
    > * How write the analogous of onchange="foo(this)" in plain JavaScript
    >
    > I think that doesn't work because the non-Prototype code, which is the
    > key of my question, has the concepts wrong. I could reproduce this
    > with no Prototype code. Just attaching that to window.onload and
    > iterating over elements of autofit class with regular calls.
    >
    > -- fxn


    OK, I narrowed that problem to an even more simple and pure JavaScript
    example, see
    http://groups.google.com/group/comp...bfb63e2375a/a9d97ed21d66de9a#a9d97ed21d66de9a.
    fxn, Jun 27, 2007
    #4
    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. Patrick

    Delay chain

    Patrick, Dec 21, 2004, in forum: VHDL
    Replies:
    1
    Views:
    1,447
    Mike Treseler
    Dec 21, 2004
  2. ivan_r_moore

    COM event callbacks

    ivan_r_moore, Nov 5, 2003, in forum: Python
    Replies:
    0
    Views:
    317
    ivan_r_moore
    Nov 5, 2003
  3. glennpierce

    multiple callbacks per function pointer / event

    glennpierce, Feb 14, 2005, in forum: C Programming
    Replies:
    1
    Views:
    344
    Ulrich Eckhardt
    Feb 14, 2005
  4. Paul F

    event chain in asp.net

    Paul F, Jul 20, 2008, in forum: ASP .Net
    Replies:
    2
    Views:
    498
    bruce barker
    Jul 20, 2008
  5. Tim Conner
    Replies:
    1
    Views:
    154
    Jason Roelofs
    Mar 3, 2008
Loading...

Share This Page