PDA

View Full Version : add a function to an event


pitchoo
06-25-2002, 03:01 PM
I use this code to add a function for the onchange event:

obj.onchange = function () { calculateExpr(expression,fieldType) }

but how can I add this function without deleting the preceding functions defined in this event?
I tried with += but it doesn't work...

Thanks

whackaxe
06-25-2002, 03:28 PM
if your funtions dont return anything then oyu could try this

function changeevent()
{
//first function you want to put in
[etc...]

//second function you wanted to put in
[etc...]

//third function you wanted to put in
[etc...]
}

obj.onchange = changeevent()

pitchoo
06-25-2002, 03:45 PM
It should be something like this:


obj.onchange = function () { obj.onchange ;calculateExpr(expression,fieldType) }


but how can obj.onchange be interpreted as the content of hum ..."obj.onchange"? Is it clear?

whackaxe
06-25-2002, 03:56 PM
sorry i dont follow what you mean

but how can obj.onchange be interpreted as the content of hum ..."obj.onchange"? Is it clear?

pitchoo
06-25-2002, 04:06 PM
I'll try to be clearer:

I have an object which is composed of an onchange event.
This event is composed of function A.

Now I need to add dynamically a second function to the object's onchange event: function B.

So I do this:

obj.onchange = function() {functionB;};

But in this case function A is logically deleted, so my question is how can I add function B and keep function A in the event?

jkd
06-25-2002, 05:19 PM
obj.addEventListener('change', myListener, false);
// for Gecko (NS6, NS7, Mozilla, Beonex, K-meleon, and Galeon), and other DOM2 browsers

obj.attachEvent('onchange', myListener);
// for IE

There is one way, or you could try:

obj.oldOnchange = obj.onchange;
obj.onchange = function() {
//my normal code
obj.oldOnchange();
}

pitchoo
06-26-2002, 09:07 AM
I have a last question about the 2 methods: addEventListener and attachEvent, which one corresponds to a JavaScript standard? Is it IE's method or Gecko's method?

pitchoo
06-26-2002, 10:04 AM
http://webfx.nu/dhtml/ieemu/eventlisteners.html

jkd
06-26-2002, 05:46 PM
Gecko's method of course - addEventListener is defined in the DOM2 Events specs.

document.implementation.hasFeature('Events','2.0')

is true in Gecko, false in IE. :)