Hi I need to create a form and add elements to it programatically.
$form = $("<form></form>");
$form.append("<input type=button value=button");
this doesn't seem to work right.
Hi I need to create a form and add elements to it programatically.
$form = $("<form></form>");
$form.append("<input type=button value=button");
this doesn't seem to work right.
The 2nd line should be written as:
$form.append('<input type="button" value="button">');
.append('<input type="button" value="button" />');input elements don't admit content.var form = $("<form/>",
{ action:'/myaction' }
);
form.append(
$("<input>",
{ type:'text',
placeholder:'Keywords',
name:'keyword',
style:'width:65%' }
)
);
form.append(
$("<input>",
{ type:'submit',
value:'Search',
style:'width:30%' }
)
);
$("#someDivId").append(form);
this looks like javascript -- The jQuery library on top of JavaScript. And it is building html markup, then injecting it into the html document on the fly. And that is the point of the OP - to do as much as possible in jQuery.function setValToAssessment(id)
{
$.getJSON("<?= URL.$param->module."/".$param->controller?>/setvalue",{id: id}, function(response)
{
var form = $('<form></form>').attr("id",'hiddenForm' ).attr("name", 'hiddenForm');
$.each(response,function(key,value){
$("<input type='text' value='"+value+"' >")
.attr("id", key)
.attr("name", key)
.appendTo("form");
});
$('#hiddenForm').appendTo('body').submit();
// window.location.href = "<?=URL.$param->module?>/assessment";
});
}
.appendTo("form");. it should be appendTo(form); Using Jquery
Rather than creating temp variables it can be written in a continuous flow pattern as follows:
$('<form/>', { action: url, method: 'POST' }).append(
$('<input>', {type: 'hidden', id: 'id_field_1', name: 'name_field_1', value: val_field_1}),
$('<input>', {type: 'hidden', id: 'id_field_2', name: 'name_field_2', value: val_field_2}),
).appendTo('body').submit();