Skip to content Skip to sidebar Skip to footer

Dynamically Create A Html Form With Javascript

how can I go about creating a form with javascript? I have NO idea on how to proceed, I have been googling my face off but there is nothing definitive that can show me how to dynam

Solution 1:

You could try something like this:

The HTML part:

<html><head></head><body><body></html>

The javascript:

<script>//create a formvar f = document.createElement("form");
f.setAttribute('method',"post");
f.setAttribute('action',"submit.php");

//create input elementvar i = document.createElement("input");
i.type = "text";
i.name = "user_name";
i.id = "user_name1";

//create a checkboxvar c = document.createElement("input");
c.type = "checkbox";
c.id = "checkbox1";
c.name = "check1";

//create a buttonvar s = document.createElement("input");
s.type = "submit";
s.value = "Submit";

// add all elements to the form
f.appendChild(i);
f.appendChild(c);
f.appendChild(s);

// add the form inside the body
$("body").append(f);   //using jQuery ordocument.getElementsByTagName('body')[0].appendChild(f); //pure javascript</script>

This way you can create as many elements as you want dynamically.

Solution 2:

I think posting a complete solution would be too much, but check out jQuery for this. I give you a hint, jQuery's .append() could be very useful for you :)

Solution 3:

My idea is that you can use the dform jquery plugin from github to create forms directly by giving input as json data.

Solution 4:

Yes, you can create any amount of html including forms by running JavaScript,

Perhaps:

<html><body><h1>My Form</h1><divid="formcontent"></div></body></html>

Our JavaScript might look like:

var el = document.createElement("input");
el.type = "text";
el.name = "myformvar";

var cont = document.getElementById("formcontent")
cont.appendChild(el);

Solution 5:

use document.write or if you want more flexibility use jquery

Post a Comment for "Dynamically Create A Html Form With Javascript"