IE's Dummy attachEvent?

H

hifchan

I'd like to attach an event to a group of possible controls, each
event's handling function might exepect different parameters. By
learning IE's attachEvent, I found no way to pass parameters.

My situation is I'm using XML to dynamically generate the event
function, inside of XML data there is a attribute specify what
associates with a particular control and parameter(s). Doing things
like this,

objElement.attachEvent("onclick", eventFunc)

I'm completely out of wood.

There are some discussions suggesting to detect srcElement, but that's
AFTER the function is created.
 
A

Alexis Nikichine

I'd like to attach an event to a group of possible controls, each
event's handling function might exepect different parameters. By
learning IE's attachEvent, I found no way to pass parameters.

My situation is I'm using XML to dynamically generate the event
function, inside of XML data there is a attribute specify what
associates with a particular control and parameter(s). Doing things
like this,

objElement.attachEvent("onclick", eventFunc)

I'm completely out of wood.

There are some discussions suggesting to detect srcElement, but that's
AFTER the function is created.

Try this:

function makeEventFunc( param1, param2 )
{
return function()
{
alert( "param1 = " + param1 + "\nparam2 = " + param2 );
}
}

objElement.attachEvent( "onclick", makeEventFunc( param1, param2 ) );

That way, makeEventFun will return you a closure, i.e. a function who
"remembers" what values were bound to param1 and param2. This "function"
can then be passed to attachEvent.

Hope this helps,

Alexis
 
D

Daniel Kirsch

Alexis said:
Try this:

function makeEventFunc( param1, param2 )
{
return function()
{
alert( "param1 = " + param1 + "\nparam2 = " + param2 );
}
}

objElement.attachEvent( "onclick", makeEventFunc( param1, param2 ) );

Nice trick!
 

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

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,677
Members
48,796
Latest member
Greg L.

Latest Threads

Top