I am building a form dynamically from json using jQuery, and I want to add input fields to my form (e.g. Name, Age, Address, Email).
Here is my code so far:
$(function () {
//Form JSON
var _form=
{
"sample_form":
{
'Title': 'Form using Jquery/JSON',
'id': 'test_form',
'action': 'thanks.html',
'method': 'post',
'fields':
[
{'id':'txtName' , 'caption':'Name' , 'type':'text' , 'required':'true'},
{'id':'txtNum' , 'caption':'Age' , 'type':'number' , 'required':'false'},
{'id':'txtAddress' , 'caption':'Address' , 'type':'address' , 'required':'false'},
{'id':'txtEmail' , 'caption':'Email' , 'type':'email' , 'required':'true'}
]
}
};
//Creating Form
$('div#form2').append(
//***********************Header*******************
$('<br/>'),
$('<span/>').hide(),
$("<h2/>").text(_form.sample_form.Title),
//***********************Form*********************
$('<form/>',
{
id: _form.sample_form.id,
action: _form.sample_form.action,
method: _form.sample_form.method
}
).append(
for (var i = 0; i <_form.sample_form.fields.length ; i++)
{
$('<input/>',{ type: i.type , required: i.required , id: i.id})
}
)
);});