Dynamic function

Given this form declaration:

<form action="upload.php" method="post"  enctype="multipart/form-data" onsubmit="return AIM.submit(this, {'onStart' : startCallback, 'onComplete' : completeCallback})">

How do I write the onsubmit dynamically? I have:

      var f = document.createElement("form");
      f.action = "upload.php";
      f.method = "post";
      f.enctype = "multipart/form-data";
      f.onsubmit = function () { return AIM.submit(f, {'onStart' : startCallback, 'onComplete' : completeCallback}); };

But that doesn’t work. What am I missing here?



Not sure what you mean by “dynamically”.

If you have this:

<form action="upload.php" method="post" enctype="multipart/form-data" id="upload_form">
  <!-- form stuff -->

Then the javascript can access it like so:

var theForm = document.getElementById('upload_form');
theForm.onsubmit = function() {
  return AIM.submit(this, {'onStart' : startCallback, 'onComplete' : completeCallback});

Now you can change whatever you want in the arguments, if that’s what you mean.

I didn’t explain very well. I want to create the form at runtime, so I am basically “translating” the HTML version of the form tag into the appropriate javascript. That’s easy enough for the “action”, “method”, and “enctype” properties, but I can’t find the appropriate syntax for the onsubmit function.

In the HTML tag version, the onsubmit declaration uses “this” (to refer to the form in question), in my example javascript, I replaced “this” with “f” (which names the form object I am creating), but the function does not work. I suspect this has to do with the names of the local functions it calls (i.e. ‘startCallback’ and ‘completeCallback’) but I don’t know how to rewrite those calls appropriately.

Does that make sense now?


Please ignore my question. The problem was elsewhere. The onsubmit function works fine.

Sorry for the bother.