/    Sign up×
Community /Pin to ProfileBookmark

"adding" two functions to handle one event

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;
}
}
?

to post a comment
JavaScript

5 Comments(s)

Copy linkTweet thisAlerts:
@CharlesJul 21.2003 — [font=georgia]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. [i]Id est[/i]: [font=monospace]formobj.elements[i].oldonchange = formobj.elements[i].onchange[/font] and such for the newonchange methods as well. [/font]
Copy linkTweet thisAlerts:
@frezauthorJul 21.2003 — 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...
Copy linkTweet thisAlerts:
@CharlesJul 21.2003 — [font=monospace]formobj.elements[i].onchange = function () {this.oldonchange(); this.newonchange()}[/font]



[font=georgia]I would expect that to work.[/font]
Copy linkTweet thisAlerts:
@frezauthorJul 21.2003 — 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...
Copy linkTweet thisAlerts:
@CharlesJul 21.2003 — [font=georgia]I do so love OOP (Object Oriented Programming).[/font]
×

Success!

Help @frez spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.16,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...