Organizational Research By

Surprising Reserch Topic

jquery create hidden form element on the fly


jquery create hidden form element on the fly  using -'javascript,jquery,forms'

asked Sep 16, 2015 by abhimca2006
0 votes
14 views



Related Hot Questions

4 Answers

0 votes
$('').attr('type','hidden').appendTo('form');

To answer your second question:

$('').attr({
    type: 'hidden',
    id: 'foo',
    name: 'bar'
}).appendTo('form');
answered Sep 16, 2015 by virendra.bajaj
0 votes

if you want to add more attributes just do like:

$('').attr('type','hidden').attr('name','foo[]').attr('value','bar').appendTo('form');

Or

$('').attr({
    type: 'hidden',
    id: 'foo',
    name: 'foo[]',
    value: 'bar'
}).appendTo('form');
answered Sep 16, 2015 by rajnipancholi
0 votes

Working JSFIDDLE

If your form is like


      First name: 
Last name:


You can add hidden input and textarea to form like this

$(document).ready(function(){

    $("#add-input").on('click', function(){
        $('#hidden-element-test').prepend('');
        alert('Hideen Input Added.');
    });

    $("#add-textarea").on('click', function(){
        $('#hidden-element-test').prepend('');
        alert('Hideen Textarea Added.');
    });

});

Check working jsfiddle here

answered Sep 16, 2015 by rajeevr642
0 votes
function addHidden(theForm, key, value) {
    // Create a hidden input element, and append it to the form:
    var input = document.createElement('input');
    input.type = 'hidden';
    input.name = key;'name-as-seen-at-the-server';
    input.value = value;
    theForm.appendChild(input);
}

// Form reference:
var theForm = document.forms['detParameterForm'];

// Add data:
addHidden(theForm, 'key-one', 'value');
answered Sep 16, 2015 by sujata naik

...