1

I am creating a dynamic form using following code,

function createForm() {
    var f = document.createElement("form");

    f.setAttribute('method',"post");
    f.setAttribute('action',"./Upload");
    f.setAttribute('name',"initiateForm");
    f.acceptCharset="UTF-8";

    var name = document.createElement("input");
    name.setAttribute('type',"text");
    name.setAttribute('name',"projectname");
    name.setAttribute('value',"saket");

    f.appendChild(name);

    f.submit();

}

But in Mozilla nothing happens but code works as expected ( in chrome). This code is being called by another function which is invoked by button on click event. After executing this code i am returning false.

Please help me out. Thanks in advance :-)

antyrat
  • 27,479
  • 9
  • 75
  • 76
Voonic
  • 4,667
  • 3
  • 27
  • 58
  • 3
    Maybe because you didn't append your form to the DOM? try to add `document.body.appendChild( f );` before submit. – antyrat Jul 31 '13 at 11:05

2 Answers2

2

You need to append the new created form to the document, because it was not there on page load.

Try this:

function createForm() {
    var f = document.createElement("form");

    f.setAttribute('method',"post");
    f.setAttribute('action',"./Upload");
    f.setAttribute('name',"initiateForm");
    f.acceptCharset="UTF-8";

    var name = document.createElement("input");
    name.setAttribute('type',"text");
    name.setAttribute('name',"projectname");
    name.setAttribute('value',"saket");

    f.appendChild(name);
    document.body.appendChild(f); // added this
    f.submit();
}
Sergio
  • 28,539
  • 11
  • 85
  • 132
-1

I had similar error just resolved the same.
If you have just used <form></form> tag and trying to submit then it gives error in older version of mozill while it works in newer version and other browsers.
The form tag should be under <html><body> tag. e.g. <html><body><form></form></body></html>

Krishna
  • 438
  • 5
  • 18