...

View Full Version : How to pass arguments independently or using addEventListener



Rok
09-10-2002, 11:08 AM
Well, I use addEventListener and I cannot (or at least don't know how) pass arguments to the function.

Let's say we have a function:
function warning(arg1, arg2) {alert('Argument 1: ' + arg1 + ', Argument2: ' + arg2 + '.');}

It's possible to have:
onclick="warning('my argument 1', 'my argument 1');" as an html attribut.

But I think it's not posible to do it like this (still the same function):
el.addEventListener("click", warning('my argument 1', 'my argument 1'), false);

So how can I pass arguments to the function?

jkd
09-10-2002, 01:03 PM
addEventListener requires a reference to a function. By passing arguments, you are inserting the return value of the function. An equivalent of what you want:

document.addEventListener('eventname', function(event) { doSomething('arg1', 'arg2') }, false);

Rok
09-10-2002, 01:56 PM
Cool, just what I need.
Thanx!

Rok
09-10-2002, 06:55 PM
Now that I've used
document.addEventListener("mousemove", function(event) {move (objectID)}, false);

now I cannot removeEventListener . I've tried many things:

------------------------------------------------------------------------------------
document.removeEventListener("mousemove", function(event) {move (objectID)}, false);

document.removeEventListener("mousemove", move, false);

...
------------------------------------------------------------------------------------

But none of them worked. How can I fix this?

Rok
09-10-2002, 09:55 PM
I've written a short page just for this example. Please check it out.

page (http://www2.arnes.si/~rcarl/test_addEventListener.html)

jkd
09-10-2002, 10:09 PM
removeEventListener requires the identical arguments passed to addEvent Listener.

You are using an anonymous function as the second argument, which is forever lost once used:

function(){doSomething()} == function(){doSomething()}
// false

Which means you'll have to declare your wrapper function separate:

function eventWrapper(event) {
move(objectID);
}

And pass eventWrapper into addEventListener as opposed to function(event) {move (objectID)}

Rok
09-11-2002, 01:14 AM
Well, I still have to pass an argument to a function using addEventListener.
But I've solved my problem using one simple ****** variable :).
But thnx anyway!

Bonker
07-13-2005, 01:36 PM
Hi Rok

Can you post your solution ? I have the same problem.
Thanx

Royal2000H
06-03-2007, 12:46 AM
I'm having the same issue....
does someone have a solution because
I need removeEventListener to work as well as passing variables

(sorry to bring up such an old topic)

GJay
06-04-2007, 10:24 PM
jkd's post explained it pretty well. If you have something that looks like:


element.addEventListener('click',function() { foo(id);},false);

then you need to use:


element.addEventList('click',bar=function() { foo(id);},false);
//and then
element.removeEventListener('click',bar,false);

[/code]



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum