I'm writing a bolt-on bit of javascript which is intended to capture information when a form is submitted. I need to accomodate the scenario where there may be multiple forms on a web page, none/some/all of which may already have onsubmit handlers defined....and I need to fire the original handler after calling the bolt-on code.
I've got a test case setup with 2 forms, the first has no onsubmit handler, the second a simple alertbox.
I tried the code below - but that seemed to copy the pre-existing handler from form2 into form1:
window.onload=pageinit;
function pageinit()
{
for (var x=0; x < document.forms.length; x++) {
var origSubmit=document.forms[x].onsubmit;
if (typeof origSubmit != 'function') {
document.forms[x].onsubmit=dosubmit;
} else {
document.forms[x].onsubmit = function (event) {
dosubmit(event);
return origSubmit(event);
}
}
}
}
function dosubmit(ev)
{
alert('bolt-on invoked from ' + ev.target.name);
}
Any ideas?
TIA