Search code examples
javascripthtmldommozilla

Mozilla form.submit() not working


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 :-)


Solution

  • 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();
    }