/    Sign up×
Community /Pin to ProfileBookmark

dynamically add code to existing onchange function

Hi,

I am working on some javascript to let the parent document know if something has changed in the frame.

When the frame loads, it calls the addOnChangeHandler method below which iterates through all the elements in the form and adds an onchange handler which informs the parent if a field has changed.

The part which I have a problem with (highlighted in red) is avoiding overwriting any existing onChange handlers. How can I add the fieldChanged function to an existing onChange handler?

[CODE]
function addOnChangeHandler()
{
for (nIndex = 0; nIndex < detailsForm.elements.length; nIndex++)
{
if (detailsForm.elements[nIndex].type == ‘text’ ||
detailsForm.elements[nIndex].type == ‘textarea’ ||
detailsForm.elements[nIndex].type == ‘select-one’ ||
detailsForm.elements[nIndex].type == ‘select-multiple’)
{
if (detailsForm.elements[nIndex].onchange == null)
{
detailsForm.elements[nIndex].onchange = new Function(‘fieldChanged()’);
}
else
{
[COLOR=red]detailsForm.elements[nIndex].onchange += new Function(‘fieldChanged()’);[/COLOR]
} // end-if existing onchange
}// end-if changeable field
} // end for
} // End addOnChangeHandler

function fieldChanged() {
parent.setModified(true);
}// End fieldChanged
[/CODE]

Thanks,
sabretooth

to post a comment
JavaScript

1 Comments(s)

Copy linkTweet thisAlerts:
@VladdyMay 02.2003 — use element.addEventListener (DOM Gecko) or element.attachEvent (damn IE) .
×

Success!

Help @sabretooth80 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 5.25,
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: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

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

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...