Tuesday, December 8, 2009

Extending JavaScript's onsubmit method

Suppose your page has a form, and you want to extend its onsubmit method programatically. Here's one way to go about it:

form = document.getElementById("form");
form.realSubmit = form.onsubmit;
form.onsubmit = function () {
  alert("before the original onsubmit");
  this.realSubmit();
}

(Based on Orc Scorcher's response to this thread on www.webdeveloper.com)

No comments: