user3790782 user3790782 - 2 years ago 63
jQuery Question

Pass parameters from dynamically added input fields to servlet

I have a form which has multiple input fields that will be added dynamically when user click the Add button.

I already managed to achieve this, but the problem I am facing now is I cannot pass the parameters that has been added dynamically using JavaScript function, to a servlet.

This is the code of dynamically adding multiple input fields:

<script type="text/javascript">
var counter = 2;

$("#addDynamicDivs").click(function () {
var newTextBoxDiv1 = $(document.createElement('div'))
.attr("id", 'TextBoxDiv1');
newTextBoxDiv1.attr("style",'float: left;');

var newTextBoxDiv2 = $(document.createElement('div'))
.attr("id", 'TextBoxDiv2');
newTextBoxDiv2.attr("style",'float: left;');

var newTextBoxDiv3 = $(document.createElement('div'))
.attr("id", 'TextBoxDiv3');
newTextBoxDiv3.attr("style",'float: left;');

var newTextBoxDiv4 = $(document.createElement('div'))
.attr("id", 'TextBoxDiv4');
newTextBoxDiv4.attr("style",'float: left;');

newTextBoxDiv1.after().html('<label class="inputlabel2">Speaker Name : </label>' +
'<input type="text" name="speakername" id="speakername" value="" >');
newTextBoxDiv2.after().html('<label class="inputlabel2">Speaker Country : </label>' +
'<input type="text" name="speakercountry" id="speakercountry" value="" >');
newTextBoxDiv3.after().html('<label class="inputlabel2">Speaker Company : </label>' +
'<input type="text" name="speakercompany" id="textbox" value="" >');
newTextBoxDiv4.after().html('<label class="inputlabel2">Speaker Desc : </label>' +
'<textarea name="speakerdesc" id="speakerdesc" rows="10" cols="30" ></textarea>');



It appears that the only parameters passed to the servlet are from the form, not including the parameters from the dynamically added input fields.

How can I pass all the parameters from the function to the servlet?



Answer Source

You have to include the dynamic params inside the form. You should put inside the form the div elemt where you are including the dynamic params.

    <div id="TextBoxesGroup">
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download