jQuery - Create hidden form element on the fly

Mithun Sreedharan picture Mithun Sreedharan · Mar 9, 2010 · Viewed 338.6k times · Source

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

Answer

David Hellsing picture David Hellsing · Mar 9, 2010
$('<input>').attr('type','hidden').appendTo('form');

To answer your second question:

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