4

I want to add multiple html elements dynamically on button click and get values of each. Also one can delete created element on button click. HTML elements will be textbox and other text box for phone number.

I've tried to create single text box on button click. Fiddle: https://jsfiddle.net/dvkeojqn/

HTML:

<input id="btnAdd" type="button" value="Add" />
<br />
<br />
<div id="TextBoxContainer">
 <!--Textboxes will be added here -->
</div>
<br />
<input id="btnGet" type="button" value="Get Values" />

JS:

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script type="text/javascript">
$(function () {
 $("#btnAdd").bind("click", function () {
 var div = $("<div />");
 div.html(GetDynamicTextBox(""));
 $("#TextBoxContainer").append(div);
 });
 $("#btnGet").bind("click", function () {
 var values = "";
 $("input[name=DynamicTextBox]").each(function () {
 values += $(this).val() + "\n";
 });
 alert(values);
 });
 $("body").on("click", ".remove", function () {
 $(this).closest("div").remove();
 });
});
function GetDynamicTextBox(value) {
 return '<input name = "DynamicTextBox" type="text" value = "' + value + '" />&nbsp;' +
 '<input type="button" value="Remove" class="remove" />'
}
</script>
Panther
3,3499 gold badges31 silver badges51 bronze badges
asked Jul 8, 2015 at 5:43
3
  • looks just fine - jsfiddle.net/arunpjohny/61fawqp2/1 - what is the problem Commented Jul 8, 2015 at 5:46
  • it is working, what is the issue? Commented Jul 8, 2015 at 5:47
  • I want to add multiple textboxes on button click. Currently only one is created Commented Jul 8, 2015 at 5:50

2 Answers 2

3

try replacing this function..

$(function () {
 $("#btnAdd").bind("click", function () {
 var div = $("<div />");
 div.html(GetDynamicTextBox(""));
 $("#TextBoxContainer").append(div);
 });
 $("#btnGet").bind("click", function () {
 var valuesarr = new Array(); 
 var phonearr = new Array(); 
 $("input[name=DynamicTextBox]").each(function () {
 valuesarr.push($(this).val());
 });
 $("input[name=phoneNum]").each(function () {
 phonearr.push($(this).val());
 });
 alert(valuesarr); alert(phonearr);
 });
 $("body").on("click", ".remove", function () {
 $(this).closest("div").remove();
 });
});
function GetDynamicTextBox(value) {
 return '<input name = "DynamicTextBox" type="text" value = "' + value + '" />&nbsp;<input name = "phoneNum" type="text" />&nbsp;<input type="button" value="Remove" class="remove" />';
}

and HERE is the FIDDLE.

answered Jul 8, 2015 at 5:52
0

Try to replace this JavaScript code..

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script type="text/javascript">
$(function () {
 $("#btnAdd").bind("click", function () {
 var div = $("<div />");
 div.html(GetDynamicTextBox(""));
 $("#TextBoxContainer").append(div);
 });
 $("#btnGet").bind("click", function () {
 var values = "";
 $("input[name=DynamicTextBox]").each(function () {
 values += $(this).val() + "\n";
 });
 alert(values);
 });
 $("body").on("click", ".remove", function () {
 $(this).closest("div").remove();
 });
});
function GetDynamicTextBox(value) {
 return '<input name = "DynamicTextBox" type="text" value = "' + value + '" />&nbsp;<input name = "DynamicTextBox" type="text" value="'+ value +'" />&nbsp;<input type="button" value="Remove" class="remove" />';
}
</script>
Nehil Mistry
1,1092 gold badges23 silver badges51 bronze badges
answered Jul 8, 2015 at 6:05

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.