What is the simplest way to dynamically create a hidden input form field using jQuery?
Inserting the <input type='hidden'> can also be done using jQuery. The append() and appendTo() inbuilt function can be used to add the hidden form element but they are not only limited to the <input type='hidden'> but we can also add other html elements. Note: They both perform pretty much the same.
In jQuery to set a hidden field value, we use . val() method.
The <input type="hidden"> defines a hidden input field. A hidden field lets web developers include data that cannot be seen or modified by users when a form is submitted.
$('<input>').attr('type','hidden').appendTo('form');
To answer your second question:
$('<input>').attr({ type: 'hidden', id: 'foo', name: 'bar' }).appendTo('form');
$('#myformelement').append('<input type="hidden" name="myfieldname" value="myvalue" />');
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With