Click to See Complete Forum and Search --> : "adding" two functions to handle one event


frez
07-21-2003, 02:49 PM
here's the challenge:
1) onLoad, I'm running a function that provides an onChange handler for all form elements because I want something to happen if and only if on the elements is changed. What's supposed to happen is a button changes from saying "Done" to saying "Cancel"

2) however, the original form contains just a handful of form elements that already have onChange handlers.

3) because my script runs onLoad, it *replaces* any previous onChange handlers with my new handler, instead of *appending* it (or preceding it or whatever - as long as all handlers happen, it doesn't matter in what order they happen)

4) so I've been trying to write the onLoad function so that it does something like "for any element that has an onChange handler, take the previous handler and combine it with this new one". I can't figure out how to do this.

4b) It is a possiblity to simply say "for any element that has an onChange handler, just skip" and then I'll manually add the new, onLoad handler to those elements. But if it's possible, I'd like a more elegant solution.

Can anyone please help? Here's my floundering script below:


function DoneCancel(formobj, buttobj1) {
for (i=0; i<formobj.length; i++) {
oldonchange = formobj.elements[i].onchange;
newonchange = function() {buttobj1.value = "Cancel"};
if (oldonchange) allonchange = function() {oldonchange; newonchange;};
else allonchange = function() {newonchange;};
formobj.elements[i].onchange = allonchange;
}
}
:confused:

Charles
07-21-2003, 03:03 PM
Currently you are parking each of the old onchange handlers in the same method of the window object. You need to park each one as a method of that particular element object. Id est: formobj.elements[i].oldonchange = formobj.elements[i].onchange and such for the newonchange methods as well.

frez
07-21-2003, 03:11 PM
hmm clearly I'm not up to par here - once I've parked them, how do I, um, drive them all at once?

And thank you much for taking the time to help me out...

Charles
07-21-2003, 03:16 PM
formobj.elements[i].onchange = function () {this.oldonchange(); this.newonchange()}

I would expect that to work.

frez
07-21-2003, 03:34 PM
ah it's so simple in retrospect. I'm embarassed at the many pathetic iterations I tried. Again, thank you very, very much. It works perfectly, as you expected...

Charles
07-21-2003, 03:47 PM
I do so love OOP (Object Oriented Programming).