how to chain event callbacks

F

fxn

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.
 
R

RobG

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
 
F

fxn

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
 
F

fxn

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top