Dynamically create form in javascript on fly

To create a form dynamically using JavaScript on fly of the webpage. This which helps to create form and input controls dynamically.

[javascript]

/* To create form with javascript */

var form = document.createElement("form");
form.setAttribute(‘method’,"post"); // form method
form.setAttribute(‘action’,"test.php"); // form action url
form.setAttribute(‘name’,"frmName"); // form name
form.setAttribute(‘id’,"frmId"); // form ID
form.setAttribute(‘target’,"_blank"); // form target location

/* To create input control with javascript */
var inputCtrl = document.createElement("input");
inputCtrl.type = "hidden"; // input control type
inputCtrl.name = "data"; // input control name
inputCtrl.value = "test"; // input control value

/* To bind created input control with created from */
form.appendChild(inputCtrl);

/* To bind created form with current document body */
document.body.appendChild(form);

/* Now you can submit created form */
form.submit();

[/javascript]

Permanent link to this article: https://blog.openshell.in/2013/12/dynamically-create-form-in-javascript-on-fly/

Leave a Reply

Your email address will not be published.