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.


/* 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 */

/* To bind created form with current document body */

/* Now you can submit created form */


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.