Here is a comparison of how to insert an HTML input of type=hidden with plain vanilla JavaScript and with jQuery.
While the plain JavaScript way is more verbose, it takes only the same number of lines as with jQuery.
In both examples, the input element will have the id and name attributes set to someID
. In both examples, the input element will be added to form with the id formElementID
.
Here is how to insert a hidden input element into an HTML form with plain JavaScript:
var elInput = document.createElement('input'); elInput.setAttribute('type', 'hidden'); elInput.id = 'someID'; elInput.setAttribute('name', 'someID'); elInput.setAttribute('value', 'some value' ); document.getElementById('formElementID').appendChild( elInput );
And here is how to do the same thing, but with jQuery:
$('<input>').attr({ type: 'hidden', id: 'someID', name: 'someID', value: 'some value' }).appendTo('#formElementID');
Questions and Comments are Welcome