i am making a program in which i want to get addition of dynamic textbox as final result. i want to add all 3rd box value in final result box,and if click on remove the value of corresponding should be deduct from final result...
here is code:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js"></script>
<ol id="boxes">
</ol>
<input type="button" value="Add a row" id="add_boxes" />
<br> final result of all 3rd boxes:
<input type="text" value="" id="final_result" />
<script>
var add = $('#add_boxes');
var all = $('#boxes');
var amountOfInputs = 2;
var maximumBoxes = 10;
add.click(function(event) {
// create a limit
if ($(".box").length >= maximumBoxes) {
alert("You cannot have more than 10 boxes!");
return;
}
var listItem = $('<li class="box"></li>');
// we will add 2 boxes here, but we can modify this in the amountOfBoxes value
for (var i = 0; i < amountOfInputs; i++) {
listItem.append('<input type="text" class="input" />');
}
listItem.append('<input type="text" class="output" name="value" placeholder="3rd box"/>');
// Lets add a link to remove this group as well, with a removeGroup class
listItem.append('<input type="button" value="Remove" class="removeGroup" />')
listItem.appendTo(all);
});
// This will tie in ANY input you add to the page. I have added them with the class `input`, but you can use any class you want, as long as you target it correctly.
$(document).on("keyup", "input.input", function(event) {
// Get the group
var group = $(this).parent();
// Get the children (all that arent the .output input)
var children = group.children("input:not(.output)");
// Get the input where you want to print the output
var output = group.children(".output");
// Set a value
var value = 0;
// Here we will run through every input and add its value
children.each(function() {
// Add the value of every box. If parseInt fails, add 0.
value += parseInt(this.value) || 0;
var a = parseInt(value);
});
// Print the output value
output.val(value);
document.getElementById('final_result').value = value;
var test = document.getElementById('final_result').value
var b = parseInt(test) + parseInt(a);
alert(a);
});
// Lets implement your remove field option by removing the groups parent div on click
$(document).on("click", ".removeGroup", function(event) {
event.preventDefault();
$(this).parent(".box").remove();
});
</script>