jQuery - Create hidden form element on the fly

Solution 1:

$('<input>').attr('type','hidden').appendTo('form');

To answer your second question:

$('<input>').attr({
    type: 'hidden',
    id: 'foo',
    name: 'bar'
}).appendTo('form');

Solution 2:

$('#myformelement').append('<input type="hidden" name="myfieldname" value="myvalue" />');