Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery - Create hidden form element on the fly

What is the simplest way to dynamically create a hidden input form field using jQuery?

like image 934
Mithun Sreedharan Avatar asked Mar 09 '10 09:03

Mithun Sreedharan


People also ask

How to create hidden field dynamically in 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.

How can add hidden field value in jQuery?

In jQuery to set a hidden field value, we use . val() method.

How do you make a hidden input in HTML?

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.


2 Answers

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

To answer your second question:

$('<input>').attr({     type: 'hidden',     id: 'foo',     name: 'bar' }).appendTo('form'); 
like image 81
David Hellsing Avatar answered Sep 20 '22 19:09

David Hellsing


$('#myformelement').append('<input type="hidden" name="myfieldname" value="myvalue" />'); 
like image 45
Mark Bell Avatar answered Sep 17 '22 19:09

Mark Bell