This question already has answers here:
How to add two strings as if they were numbers? [duplicate]
(20 answers)
Closed 3 years ago.
I want to add all input fields values. Some of the fields are constant and some too you have to click on a button to add them and calculate the values.
The add button when clicked adds the input fields correctly but the calculation of all the values is the problem.
$(document).on("keyup", ".price", function() {
var closestParent = $(this).closest('tr');
var total = closestParent.find(".price").val();
var tuition = document.getElementById("tuition").value;
var pta = document.getElementById("pta").value;
var transport = document.getElementById("transport").value;
var totals = parseInt(total);
var t = 0;
$('.price').each(function(i, e) {
var amt = $(this).val() - 0;
var z = t += amt;
var totalz = z + tuition + transport + pta;
document.getElementById("tot").value = tuition;
console.log(t);
console.log(tuition)
})
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table>
<tbody>
<tr>
<td style="width: 60%;">
<h5>Tuition Fees </h5>
</td>
<td>
<input class="form-control" id="tuition" name="tuition_fees" type="text" placeholder="Enter Tuition Fees..." value="" style="border:none;" />
</td>
</tr>
<tr>
<td style="width: 60%;">
<h5>PTA Dues </h5>
</td>
<td>
<input class="form-control" id="pta" name="PTA_dues" type="text" placeholder="Enter PTA Dues Amount..." value="" style="border:none;" />
</td>
</tr>
<tr>
<td style="width: 60%;">
<h5>Transport Fares </h5>
</td>
<td>
<input class="form-control" id="transport" name="transport_fares" type="text" placeholder="Enter Transport Fare..." value="" style="border:none;" />
</td>
</tr>
<tr>
<td colspan="2">
<h5>Additional Fees </h5>
</td>
</tr>
</tbody>
</table>
<table class="table table-bordered table-hover table-striped" id="invoiceItem">
<tbody>
<tr>
<td style="width: 60%;">
<div class="row">
<div class="col-md-1"><input class="itemRow" type="checkbox"></div>
<div class="col-md-10"><input type="text" style="border:none;" name="productCode[]" placeholder="Enter Fees Name" id="productCode_1" class="form-control price" autocomplete="off"></div>
</div>
</td>
<td><input type="text" name="productName[]" style="border:none;" placeholder="Enter Amount" id="price_" class="form-control" autocomplete="off"></td>
</tr>
</tbody>
</table>
<div class="row">
<div class="col-xs-12 col-sm-3 col-md-3 col-lg-3">
<button class="btn btn-danger delete" id="removeRows" type="button">- Delete item</button>
<button class="btn btn-success" id="addRows" type="button">+ Add item</button>
</div>
</div>
<br/>
<table class="table table-bordered">
<tr>
<td style="width:60%;">
<h5>TOTAL FEES </h5>
</td>
<td><input class="form-control" id="tot" name="total_fees" type="text" placeholder="total fees" value="" style="border:none;" /></td>
</tr>
</table>
What I want to do is get the total summation of the values in the total fees input field
parse your textbox values using parseInt. then you are redefining t everytime so I edited it. then move your class name price from enter fee name to enter amount textbox.
$(document).on("keyup",".price",function(){
var closestParent = $(this).closest('tr');
var total = parseInt(closestParent.find(".price").val());
var tuition = parseInt(document.getElementById("tuition").value);
var pta = parseInt(document.getElementById("pta").value);
var transport = document.getElementById("transport").value;
var totals = parseInt(total);
var t = 0;
$('.price').each(function(i,e){
var amt =$(this).val()-0;
t += amt;
var z = t;
var totalz = z + tuition + transport + pta;
document.getElementById("tot").value=tuition;
console.log(t);
console.log(tuition)
})
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<tbody>
<tr>
<td style="width: 60%;">
<h5>Tuition Fees </h5>
</td>
<td>
<input class="form-control" id="tuition" name="tuition_fees" type="text"
placeholder="Enter Tuition Fees..." value="" style="border:none;" />
</td>
</tr>
<tr>
<td style="width: 60%;">
<h5>PTA Dues </h5>
</td>
<td>
<input class="form-control" id="pta" name="PTA_dues" type="text"
placeholder="Enter PTA Dues Amount..." value="" style="border:none;" />
</td>
</tr>
<tr>
<td style="width: 60%;">
<h5>Transport Fares </h5>
</td>
<td>
<input class="form-control" id="transport" name="transport_fares" type="text"
placeholder="Enter Transport Fare..." value="" style="border:none;" />
</td>
</tr>
<tr>
<td colspan="2" >
<h5>Additional Fees </h5>
</td>
</tr>
</tbody>
</table>
<table class="table table-bordered table-hover table-striped" id="invoiceItem">
<tbody>
<tr>
<td style="width: 60%;"><div class="row">
<div class="col-md-1"><input class="itemRow" type="checkbox"></div>
<div class="col-md-10"><input type="text" style="border:none;" name="productCode[]" placeholder="Enter Fees Name" id="productCode_1" class="form-control" autocomplete="off"></div></div></td>
<td><input type="text" name="productName[]" style="border:none;" placeholder="Enter Amount" id="price_" class="form-control price" autocomplete="off"></td>
</tr>
</tbody>
</table>
<div class="row">
<div class="col-xs-12 col-sm-3 col-md-3 col-lg-3">
<button class="btn btn-danger delete" id="removeRows" type="button">- Delete item</button>
<button class="btn btn-success" id="addRows" type="button">+ Add item</button>
</div>
</div>
<br/>
<table class="table table-bordered">
<tr>
<td style="width:60%;" >
<h5>TOTAL FEES </h5>
</td>
<td><input class="form-control" id="tot" name="total_fees" type="text"
placeholder="total fees" value="" style="border:none;" /></td>
</tr>
</table>
Related
I have a table from which I need to select all of the values of the row after an event done in the same row (in this case is a keyup event) with jQuery. The table could have many rows but the selection has to be on the items of the row event.
This is the HTML of the table:
<table class="table" id="form-table">
<thead>
<tr>
<th scope="col">#</th>
<th scope="col">Description</th>
<th scope="col">Qty</th>
<th scope="col">Price</th>
<th scope="col">Discount</th>
<th scope="col">Amount</th>
<th scope="col">Hidden</th>
</tr>
</thead>
<tbody>
<tr id="row-1">
<th scope="row">1</th>
<td class="description-column">
<div class="form-group">
<select class="form-control" id="companySelect">
{% for product in product_options %}
<option>{{ product }}</option>
{% endfor %}
</select>
</div>
</td>
<td class="qty-column">
<div class="form-group">
<input type="number" class="form-control" id="qty" placeholder="Qty" min="0" step="1">
</div>
</td>
<td class="price-column">
<div class="form-group">
<input type="number" class="form-control" id="price" min="0.01" placeholder="Price">
</div>
</td>
<td class="discount-column">
<div class="form-group">
<input type="number" class="form-control" id="discount" placeholder="0" min="0.01" max="99.99">
</div>
<div>%</div>
</td>
<td class="amount-column"></td>
<td class="hidden-column">
<div class="form-check">
<input class="form-check-input" type="checkbox" value="" id="hiddenCheck">
</div>
</td>
</tr>
</tbody>
</table>
And looks like this on the site:
For more context: I am making a quote app in which I'll type the price and quantity of each item in each row and I need to update the "amount" column after each keyup. So I need to know how to select the proper "qty", "price", "discount" and "amount" id's of the row typed to do that.
Firstly, The id attribute is unique, So you should replace it with class like this
<input type="number" class="form-control qty" placeholder="Qty" min="0" step="1">
Secondly, You can get the tr by using .closest() like this $(this).closest("tr")
Finally, You can calculate individual row by creating another method named updateAmount_per_row like below:
function updateAmount_per_row(tr){
var qTy = getValue_by_className(tr, '.qty-column .qty');
var price = getValue_by_className(tr, '.price-column .price');
var amount = qTy * price;
tr.find('.amount-column').html(amount);
}
$(".qty-column, .price-column").on("keyup", function(){
var tr = $(this).closest("tr");
updateAmount_per_row(tr);
});
function updateAmount_per_row(tr){
var qTy = getValue_by_className(tr, '.qty-column .qty');
var price = getValue_by_className(tr, '.price-column .price');
var amount = qTy * price;
tr.find('.amount-column').html(amount);
}
function getValue_by_className(tr, className){
var value = parseInt(tr.find(className).val(), 10);
return value >= 0 ? value : 1;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table class="table" id="form-table">
<thead>
<tr>
<th scope="col">#</th>
<th scope="col">Description</th>
<th scope="col">Qty</th>
<th scope="col">Price</th>
<th scope="col">Discount</th>
<th scope="col">Amount</th>
<th scope="col">Hidden</th>
</tr>
</thead>
<tbody>
<tr id="row-1">
<th scope="row">1</th>
<td class="description-column">
<div class="form-group">
<select class="form-control" class="companySelect">
<option>Product A</option>
<option>Product B</option>
</select>
</div>
</td>
<td class="qty-column">
<div class="form-group">
<input type="number" class="form-control qty" placeholder="Qty" min="0" step="1">
</div>
</td>
<td class="price-column">
<div class="form-group">
<input type="number" class="form-control price" min="0.01" placeholder="Price">
</div>
</td>
<td class="discount-column">
<div class="form-group">
<input type="number" class="form-control" class="discount" placeholder="0" min="0.01" max="99.99">
</div>
<div>%</div>
</td>
<td class="amount-column"></td>
<td class="hidden-column">
<div class="form-check">
<input class="form-check-input" type="checkbox" value="" id="hiddenCheck">
</div>
</td>
</tr>
<tr id="row-2">
<th scope="row">2</th>
<td class="description-column">
<div class="form-group">
<select class="form-control" class="companySelect">
<option>Product A</option>
<option>Product B</option>
</select>
</div>
</td>
<td class="qty-column">
<div class="form-group">
<input type="number" class="form-control qty" placeholder="Qty" min="0" step="1">
</div>
</td>
<td class="price-column">
<div class="form-group">
<input type="number" class="form-control price" min="0.01" placeholder="Price">
</div>
</td>
<td class="discount-column">
<div class="form-group">
<input type="number" class="form-control" class="discount" placeholder="0" min="0.01" max="99.99">
</div>
<div>%</div>
</td>
<td class="amount-column"></td>
<td class="hidden-column">
<div class="form-check">
<input class="form-check-input" type="checkbox" value="" id="hiddenCheck">
</div>
</td>
</tr>
</tbody>
</table>
Using Mai Phuong answer as testing I managed to make it work by using the target property instead of this, because this for some reason was referencing the whole document (#document).
Here is the keyup event listener:
$('.price, .qty, .discount').keyup((event) => {
let tr = event.target.closest('tr');
updateAmount(tr);
});
and here's the final updateAmount function:
function updateAmount(tr) {
let qty = $(tr).find('.qty').val();
let price = $(tr).find('.price').val();
let discount = $(tr).find('.discount').val();
let amount = (qty * price * (100 - discount)) / 100;
$(tr).find('.amount').html(amount);
}
Hello I have developed a form where input boxes value of table should not be greater then input box outside of table.
<form>
<div class="form-group">
<label class="label1 col-md-4">Total number of sanctioned seats
<span class="required"> * </span>
</label>
<div class="col-md-7">
<input type="text" class="form-control" id="sanctionedSeatsSummary">
</div>
</div>
<table class="eduleveles table table-bordered table-hover">
<thead>
<th></th>
<th>Faculty</th>
<th>Enter sanctioned seats</th>
</thead>
<tbody>
<tr>
<td>
<input type="checkbox" name="Check" class="cbxenable">
</td>
<td>
</td>
<td>
<input type="text" class="form-control seats" name="seats">
</td>
</tr>
<tr>
<td>
<input type="checkbox" name="Check" class="cbxenable">
</td>
<td>
</td>
<td>
<input type="text" class="form-control seats" name="seats">
</td>
</tr>
<tr>
<td>
<input type="checkbox" name="Check" class="cbxenable">
</td>
<td>
</td>
<td>
<input type="text" class="form-control seats" name="seats">
</td>
</tr>
</tbody>
</table>
</form>
in above jsfiddle all ENTER SANCTIONED SEATS sum will not be greater then "Enter total number of sanctioned seats" in onchange.
You need to use jquery keyup event as I mentioned in fiddle.
Please check this fiddle
Here I just made textbox value blank if sum of three sanctioned seats are greater than total.
Also i have cleare 3 textbox if you chane total textbox
Open this link : https://jsfiddle.net/5y6na3wr/7/
$(document).ready(function(){
$(".seats").on('keyup',function(){
var total = parseInt(0) ;
$( ".seats" ).each(function( index ) {
if($(this).val()){
total = total + parseInt($(this).val());
}
});
if(total > $("#sanctionedSeatsSummary").val()){
alert("total sanctioned Seats are greater then given sanctioned Seats");
}
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<form>
<div class="form-group">
<label class="label1 col-md-4">Total number of sanctioned seats
<span class="required"> * </span>
</label>
<div class="col-md-7">
<input type="text" class="form-control" id="sanctionedSeatsSummary">
</div>
</div>
<table class="eduleveles table table-bordered table-hover">
<thead>
<th></th>
<th>Faculty</th>
<th>Enter sanctioned seats</th>
</thead>
<tbody>
<tr>
<td>
<input type="checkbox" name="Check" class="cbxenable">
</td>
<td>
</td>
<td>
<input type="text" class="form-control seats" name="seats">
</td>
</tr>
<tr>
<td>
<input type="checkbox" name="Check" class="cbxenable">
</td>
<td>
</td>
<td>
<input type="text" class="form-control seats" name="seats">
</td>
</tr>
<tr>
<td>
<input type="checkbox" name="Check" class="cbxenable">
</td>
<td>
</td>
<td>
<input type="text" class="form-control seats" name="seats">
</td>
</tr>
</tbody>
</table>
</form>
check below snippet
$(document).ready(function(){
var ttl, val;
$("input[name=seats]").on('keyup', function(){
val = 0;
$("input[name=seats]").each(function(){
if(parseInt($(this).val().trim()) > 0)
val += parseInt($(this).val().trim());
});
ttl = parseInt($("#sanctionedSeatsSummary").val().trim());
if(val > ttl)
alert("your alert");
});
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form>
<div class="form-group">
<label class="label1 col-md-4">Total number of sanctioned seats
<span class="required"> * </span>
</label>
<div class="col-md-7">
<input type="text" class="form-control" id="sanctionedSeatsSummary">
</div>
</div>
<table class="eduleveles table table-bordered table-hover">
<thead>
<th></th>
<th>Faculty</th>
<th>Enter sanctioned seats</th>
</thead>
<tbody>
<tr>
<td>
<input type="checkbox" name="Check" class="cbxenable">
</td>
<td>
</td>
<td>
<input type="text" class="form-control seats" name="seats">
</td>
</tr>
<tr>
<td>
<input type="checkbox" name="Check" class="cbxenable">
</td>
<td>
</td>
<td>
<input type="text" class="form-control seats" name="seats">
</td>
</tr>
<tr>
<td>
<input type="checkbox" name="Check" class="cbxenable">
</td>
<td>
</td>
<td>
<input type="text" class="form-control seats" name="seats">
</td>
</tr>
</tbody>
</table>
</form>
I would like to append a new row of 5 input tags to the <td> element, which is a child of <tr class="unit">, and which is a grandchild of <table id="myTable"> element.
In my Javascript code, I was able to console-log the 5 input tags, but was not able to append them to the <td> element and to the rest of the parent elements.
Is my code on the right track for solving this?
Here's a link to the what I'm doing http://codepen.io/johnnyginbound/pen/xZxZNo?editors=101
function addRow(){
var parentTable = document.getElementById('myTable');
var tableRow = document.getElementsByTagName('tr')[0].children;
var unitTables = document.getElementsByClassName('unit-table')[0];
var newInputType = document.createElement('input');
newInputType.setAttribute('type', 'text');
for(var i=0; i<unitTables.children.length; i++){
var appendedInput = unitTables.children[i].appendChild(newInputType)
parentTable.appendChild(appendedInput);
}
}
document.getElementById('add_btn').onclick=addRow;
<form>
<div class="panel panel-default">
<!-- Default panel contents -->
<div class="panel-heading">Panel heading</div>
<div class="panel-body">
<p> ASME Email: </p>
<input type="text" name="email">
<br />
<br />
</div>
<!-- Table -->
<table id="myTable" class="table">
<tr>
<th> Technology </th>
<th> Markets </th>
<th> Enduring/Emerging </th>
<th> ASME Relevency </th>
<th> Comments </th>
</tr>
<tr class="unit-table">
<td>
<input type="text">
</td>
<td>
<input type="text">
</td>
<td>
<input type="text">
</td>
<td>
<input type="text">
</td>
<td>
<input type="text">
</td>
</tr>
<tr class="unit-table">
<td>
<input type="text">
</td>
<td>
<input type="text">
</td>
<td>
<input type="text">
</td>
<td>
<input type="text">
</td>
<td>
<input type="text">
</td>
</tr>
<tr class="unit-table">
<td>
<input type="text">
</td>
<td>
<input type="text">
</td>
<td>
<input type="text">
</td>
<td>
<input type="text">
</td>
<td>
<input type="text">
</td>
</tr>
</table>
</div>
<button id="add_btn">Add new row</button>
<input type="submit" name="submit" value="Submit">
</form>
Your problem is the form tag which refresh your Web in each button event. Also your JavaScript code have several problems.
So your code should be
<div class="panel panel-default">
<!-- Default panel contents -->
<div class="panel-heading">Panel heading</div>
<div class="panel-body">
<p> ASME Email: </p>
<input type="text" name="email">
<br />
<br />
</div>
<!-- Table -->
<table id="myTable" class="table">
<tr>
<th> Technology </th>
<th> Markets </th>
<th> Enduring/Emerging </th>
<th> ASME Relevency </th>
<th> Comments </th>
</tr>
<tr class="unit-table">
<td>
<input type="text">
</td>
<td>
<input type="text">
</td>
<td>
<input type="text">
</td>
<td>
<input type="text">
</td>
<td>
<input type="text">
</td>
</tr>
<tr class="unit-table">
<td>
<input type="text">
</td>
<td>
<input type="text">
</td>
<td>
<input type="text">
</td>
<td>
<input type="text">
</td>
<td>
<input type="text">
</td>
</tr>
<tr class="unit-table">
<td>
<input type="text">
</td>
<td>
<input type="text">
</td>
<td>
<input type="text">
</td>
<td>
<input type="text">
</td>
<td>
<input type="text">
</td>
</tr>
</table>
</div>
JavaScript
function addRow(){
var parentTable = document.getElementById('myTable');
var myTd,myInput;
var myTr = document.createElement('tr');
myTr.setAttribute('class','unit-table');
for (var i=0; i<5;i++){
myTd = document.createElement('td');
myInput = document.createElement('input');
myInput.setAttribute('type','text');
myTd.appendChild(myInput);
myTr.appendChild(myTd);
}
parentTable.appendChild(myTr);
}
document.getElementById('add_btn').onclick=addRow;
And your javascript and html should be as follow
function addRow(){
var table = document.getElementById("myTable");
var rows = document.getElementById("myTable").rows.length;
var table = document.getElementById("myTable");
var rows = document.getElementById("myTable").rows.length;
// add row
var row = table.insertRow(rows);
// add input in cell
for(var i = 0; i < 5; i++){
var cell1 = row.insertCell(i);
var inputItem = document.createElement('input');
cell1.appendChild(inputItem);
}
}
document.getElementById('add_btn').onclick=addRow;
<form>
<div class="panel panel-default">
<!-- Default panel contents -->
<div class="panel-heading">Panel heading</div>
<div class="panel-body">
<p> ASME Email: </p>
<input type="text" name="email">
<br />
<br />
</div>
<!-- Table -->
<table id="myTable" class="table">
<tr>
<th> Technology </th>
<th> Markets </th>
<th> Enduring/Emerging </th>
<th> ASME Relevency </th>
<th> Comments </th>
</tr>
<tr class="unit-table">
<td>
<input type="text">
</td>
<td>
<input type="text">
</td>
<td>
<input type="text">
</td>
<td>
<input type="text">
</td>
<td>
<input type="text">
</td>
</tr>
<tr class="unit-table">
<td>
<input type="text">
</td>
<td>
<input type="text">
</td>
<td>
<input type="text">
</td>
<td>
<input type="text">
</td>
<td>
<input type="text">
</td>
</tr>
<tr class="unit-table">
<td>
<input type="text">
</td>
<td>
<input type="text">
</td>
<td>
<input type="text">
</td>
<td>
<input type="text">
</td>
<td>
<input type="text">
</td>
</tr>
</table>
</div>
<input type="submit" name="submit" value="Submit">
</form>
<button id="add_btn">Add new row</button>
like this
function addRow(){
var table = document.getElementById("myTable");
var rows = document.getElementById("myTable").rows.length;
// add row
var row = table.insertRow(rows);
// add input in cell
for(var i = 0; i < 5; i++){
var cell1 = row.insertCell(i);
var inputItem = document.createElement('input');
cell1.appendChild(inputItem);
}
}
This took a lot of work. Here is my answer :)
https://jsfiddle.net/www139/1jwf02p7/
function addRow() {
var table = document.getElementById('myTable');
var columnLength = table.getElementsByTagName('tr')[0].children.length;
var units = document.getElementsByClassName('unit-table');
var tr = document.createElement('tr');
tr.className = 'unit-table';
for (var i = 0; i < columnLength; i++) {
var td = document.createElement('td');
var text = document.createElement('input');
text.type = 'text';
td.appendChild(text);
tr.appendChild(td);
}
table.appendChild(tr);
}
document.getElementById('add_btn').onclick = addRow;
<!--<form>-->
<div class="panel panel-default">
<!-- Default panel contents -->
<div class="panel-heading">Panel heading</div>
<div class="panel-body">
<p>ASME Email:</p>
<input type="text" name="email">
<br />
<br />
</div>
<!-- Table -->
<table id="myTable" class="table">
<tr>
<th>Technology</th>
<th>Markets</th>
<th>Enduring/Emerging</th>
<th>ASME Relevency</th>
<th>Comments</th>
</tr>
<tr class="unit-table">
<td>
<input type="text">
</td>
<td>
<input type="text">
</td>
<td>
<input type="text">
</td>
<td>
<input type="text">
</td>
<td>
<input type="text">
</td>
</tr>
<tr class="unit-table">
<td>
<input type="text">
</td>
<td>
<input type="text">
</td>
<td>
<input type="text">
</td>
<td>
<input type="text">
</td>
<td>
<input type="text">
</td>
</tr>
<tr class="unit-table">
<td>
<input type="text">
</td>
<td>
<input type="text">
</td>
<td>
<input type="text">
</td>
<td>
<input type="text">
</td>
<td>
<input type="text">
</td>
</tr>
</table>
</div>
<button id="add_btn">Add new row</button>
<input type="submit" name="submit" value="Submit">
<!--</form>-->
I have a problem when I remove a row from my table. I have a function that calculates the total price, sub-total etc, which works fine.
However if I remove a row it will not re-calculate it i.e. remove the cost.
1) How can I fix this?
2) How can I get NaN to not appear in the "Total Price" column - FIXED
3) I am no expert with JavaScript, so also any help would be greatly appreciated on improving the existing code!
EDIT: included the x's to delete rows in code snippet (the issue is when you enter values in a row, it calculates them, then when you delete the row it doesn't take away the values from the total)
$(function() {
$(".calculate-rows").keyup(function(event) {
var total = 0;
$(".calculate-rows").each(function() {
var gtotal = 0;
$(this).find(".rows").each(function() {
var qty = parseFloat($(this).find(".quantity").val());
var rate = parseFloat($(this).find(".unit-price").val());
if (isNaN(qty)) {
qty = 0;
}
if (isNaN(rate)) {
rate = 0;
}
var subtotal = qty * rate;
var subtotal = qty * rate;
$(this).find(".total-price").val(subtotal.toFixed(2));
if (!isNaN(subtotal))
gtotal += subtotal;
$(".subtotal").html("£" + gtotal.toFixed(2));
var discount = $('.discount').val();
var discount = ((gtotal / 100) * discount);
var total = (gtotal - discount).toFixed(2);
if (!isNaN(total))
$(".total-price").html("£" + total);
});
});
});
});
var wrapper = $('#addrow');
var newitem = $('.newitem');
var removeitem = $('.removeitem');
$(newitem).click(function(e) {
e.preventDefault();
$newrow = $('<tr class="rows"><td style="border-top: none;"><input class="form-control" type="text" name="name" required></td><td style="border-top: none;"><textarea class="form-control" rows="1" name="description"></textarea></td><td style="border-top: none;"><input class="text-center form-control quantity" type="text" value="" name="quantity"></td><td style="border-top: none;"><input class="text-center form-control unit-price" type="text" value="" name="unit_price"></td><td style="border-top: none;"><input class="form-control text-center total-price" type="text" value="0.00" readonly></td><td style="border-top: none;" class="text-center"><a class="removeitem" href="#"><i class="fa fa-times"></i></a></td></tr>');
$(wrapper).append($newrow);
$newrow.on("click", "a", function(e) {
e.preventDefault();
$(this).parent().parent().remove();
});
});
$(removeitem).click(function(e) {
e.preventDefault();
$(this).parent().parent().remove();
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css">
<link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/font-awesome/4.3.0/css/font-awesome.min.css">
<div class="table-responsive calculate-rows">
<table class="table">
<thead>
<i class="fa fa-plus"></i> New Item
<tr>
<th style="width:25%;">Item</th>
<th style="width:41%;">Description</th>
<th style="width:10%;" class="text-center">Quantity</th>
<th style="width:10%;" class="text-center">Unit Price (£)</th>
<th style="width:10%;" class="text-center">Total Price (£)</th>
<th style="width:4%;"></th>
</tr>
</thead>
<tbody id="addrow">
<tr class="rows">
<td style="border-top: none;">
<input class="form-control" type="text" name="name" required>
</td>
<td style="border-top: none;">
<textarea class="form-control" rows="1" name="description"></textarea>
</td>
<td style="border-top: none;">
<input class="text-center form-control quantity" type="text" value="" name="quantity">
</td>
<td style="border-top: none;">
<input class="text-center form-control unit-price" type="text" value="" name="unit_price">
</td>
<td style="border-top: none;">
<input class="form-control text-center total-price" type="text" value="0.00" readonly>
</td>
<td style="border-top: none;" class="text-center"><a class="removeitem" href="#"><i class="fa fa-times"></i></a>
</td>
</tr>
<tr class="rows">
<td style="border-top: none;">
<input class="form-control" type="text" name="name" required>
</td>
<td style="border-top: none;">
<textarea class="form-control" rows="1" name="description"></textarea>
</td>
<td style="border-top: none;">
<input class="text-center form-control quantity" type="text" value="" name="quantity">
</td>
<td style="border-top: none;">
<input class="text-center form-control unit-price" type="text" value="" name="unit_price">
</td>
<td style="border-top: none;">
<input class="form-control text-center total-price" type="text" value="0.00" readonly>
</td>
<td style="border-top: none;" class="text-center"><a class="removeitem" href="#"><i class="fa fa-times"></i></a>
</td>
</tr>
<tr class="rows">
<td style="border-top: none;">
<input class="form-control" type="text" name="name" required>
</td>
<td style="border-top: none;">
<textarea class="form-control" rows="1" name="description"></textarea>
</td>
<td style="border-top: none;">
<input class="text-center form-control quantity" type="text" value="" name="quantity">
</td>
<td style="border-top: none;">
<input class="text-center form-control unit-price" type="text" value="" name="unit_price">
</td>
<td style="border-top: none;">
<input class="form-control text-center total-price" type="text" value="0.00" readonly>
</td>
<td style="border-top: none;" class="text-center"><a class="removeitem" href="#"><i class="fa fa-times"></i></a>
</td>
</tr>
</tbody>
</table>
<table class="table invoice-table text-right">
<tbody class="totals">
<tr>
<td style="border-top: none;">Sub Total:</td>
<td style="border-top: none;"><strong class="subtotal">£0.00</strong>
</td>
</tr>
<tr>
<td style="border-top: none;">Discount:</td>
<td style="width:20%; border-top: none;">
<div class="fm-group input-group" style="margin-bottom:0px">
<span class="input-group-addon">%</span>
<input type="number" class="form-control text-right discount" value="0">
</div>
</td>
</tr>
<tr>
<td style="border-top: none;">VAT:</td>
<td style="border-top: none;"><strong>£0</strong>
</td>
</tr>
<tr>
<td style="border-top: none;">Amount Due:</td>
<td style="border-top: none;"><strong class="total-price">£0</strong>
</td>
</tr>
</tbody>
</table>
</div>
In order for your calculation to work, when you remove a row, wrap your calculation logic in a function calculate() and call it when you remove a row.
Regarding the NaN, you just to ensure that when the text boxes for are blank for Quantity and Unit Rate, the variables qty and rate should default to 0.
$(function() {
$(".calculate-rows").keyup(function(event) {
calculate();
});
});
function calculate() {
var total = 0;
$(".calculate-rows").each(function() {
var gtotal = 0;
$(this).find(".rows").each(function() {
var qty = parseFloat($(this).find(".quantity").val());
var rate = parseFloat($(this).find(".unit-price").val());
if (isNaN(qty) ) qty = 0;
if (isNaN(rate) ) rate = 0;
var subtotal = qty * rate;
$(this).find(".total-price").val(subtotal.toFixed(2));
if (!isNaN(subtotal))
gtotal += subtotal;
$(".subtotal").html("£" + gtotal.toFixed(2));
var discount = $('.discount').val();
var discount = ((gtotal / 100) * discount);
var total = (gtotal - discount).toFixed(2);
if (!isNaN(total))
$(".total-price").html("£" + total);
});
});
}
var wrapper = $('#addrow');
var newitem = $('.newitem');
var removeitem = $('.removeitem');
$(newitem).click(function(e) {
e.preventDefault();
$newrow = $('<tr class="rows"><td style="border-top: none;"><input class="form-control" type="text" name="name" required></td><td style="border-top: none;"><textarea class="form-control" rows="1" name="description"></textarea></td><td style="border-top: none;"><input class="text-center form-control quantity" type="text" value="" name="quantity"></td><td style="border-top: none;"><input class="text-center form-control unit-price" type="text" value="" name="unit_price"></td><td style="border-top: none;"><input class="form-control text-center total-price" type="text" value="0.00" readonly></td><td style="border-top: none;" class="text-center"><a class="removeitem" href="#"><i class="fa fa-times"></i></a></td></tr>');
$(wrapper).append($newrow);
$newrow.on("click", "a", function(e) {
e.preventDefault();
$(this).parent().parent().remove();
calculate();
});
});
$(removeitem).click(function(e) {
e.preventDefault();
$(this).parent().parent().remove();
calculate();
});
Sounds like you're using an outdated jQuery object to get your values from...check updating your data sources..
try re-firing the keyup event once you add/delete row by calling $(".calculate-rows").keyup();
Im trying to make the input from the user in a html form be added to a table that adds up the total price of all the products in the same page all of this without reloading.
here is my html form and table code
Thank you in advance
<h1>Instructions</h1>
<section>
<p>Please enter the desired product/services in the following table to create an order.</p>
<section style="width:300px; margin-left:20px">
<form action="" name="order" method="POST" autocomplete="off">
<table width="300" border="0" cellspacing="5" cellpadding="2">
<tr>
<td>
<label for="product">Product:</label>
</td>
<td>
<input name="product" id="product" required pattern="[a-zA-Z ]*$" title="Please enter only alphabetic characters" type="text" placeholder="Product name" size="28" />
</td>
</tr>
<tr>
<td>
<label for="quantity">Quantity:</label>
</td>
<td>
<input name="quantity" id="quantity" required type="number" title="Enter item quantity" placeholder="Product quantity" width="196px" />
</td>
</tr>
<tr>
<td>
<label for="price">Price:</label>
</td>
<td>
<input name="price" id="price" required pattern="[0-9]" title="Please enter only numeric characters" placeholder="Product price" size="28" />
</td>
</tr>
</table>
<br>
<div id="buttons">
<input type="reset" name="reset" id="resetbtn" class="resetbtn" value="Reset">
<input type="submit" name="submit" id="submitbtn" class="submitbtn" tabindex="7" value="Submit this!" onclick="">
<br style="clear:both;">
</div>
</form>
</section>
</section>
<table width="416" border="0" cellspacing="5" cellpadding="2">
<tr>
<th width="115" scope="col">Products</th>
<th width="112" scope="col">Quantity</th>
<th width="92" scope="col">Price</th>
<th width="56"></th>
</tr>
<tr>
<td scope="col"> </th>
<td scope="col">
</th>
<td scope="col"> </th>
<th>Total</th>
</tr>
</table>
This is a simple update to what you have that works. Part of your question was to avoid page reloading, so you will notice the FORM no longer does a POST action and your SUBMIT BUTTON is no longer an input but a standard button with an onClick action. This will allow everything to execute without navigating away from the page. For the sake of time I put the results addition in a separate table, feel free to style how you wish.
<html>
<head>
<title>Order</title>
<script type="text/javascript">
var qtyTotal = 0;
var priceTotal = 0;
function updateForm() {
var product = document.getElementById("product").value;
var qty = document.getElementById("quantity").value;
qtyTotal = qtyTotal + parseInt(qty);
document.getElementById("qtyTotals").innerHTML=qtyTotal;
var price = document.getElementById("price").value;
priceTotal = priceTotal + parseInt(price);
document.getElementById("priceTotals").innerHTML=priceTotal;
var table=document.getElementById("results");
var row=table.insertRow(-1);
var cell1=row.insertCell(0);
var cell2=row.insertCell(1);
var cell3=row.insertCell(2);
cell1.innerHTML=product;
cell2.innerHTML=qty;
cell3.innerHTML=price;
}
</script>
</head>
<body>
<form name="order" id="order">
<table>
<tr>
<td>
<label for="product">Product:</label>
</td>
<td>
<input id="product" name="product" title="Please enter only alphabetic characters" type="text" size="28" />
</td>
</tr>
<tr>
<td>
<label for="quantity">Quantity:</label>
</td>
<td>
<input id="quantity" name="quantity" title="Enter item quantity" width="196px" />
</td>
</tr>
<tr>
<td>
<label for="price">Price:</label>
</td>
<td>
<input id="price" name="price" title="Please enter only numeric characters" size="28" />
</td>
</tr>
</table>
<input type="reset" name="reset" id="resetbtn" class="resetbtn" value="Reset" />
<button type="button" onClick="updateForm();"/>Add To Table</button>
</form>
<br>
<table id="results" width="360">
<thead>
<tr>
<th scope="col" width="120">Products</th>
<th scope="col" width="120">Quantity</th>
<th scope="col" width="120">Price</th>
</tr>
</thead>
</table>
<table id="resultTotals" width="360">
<tr>
<td scope="col" width="120">Totals</td>
<td scope="col" width="120"><div id="qtyTotals"></div></td>
<td scope="col" width="120"><div id="priceTotals"></div></td>
</tr>
</table>
</body></html>
Here is JS Fiddle Example of above code.
<section>
<p>Please enter the desired product/services in the following table to create an order.</p>
<section style="width:300px; margin-left:20px">
<form action="" name="order" method="POST" autocomplete="off">
<table id="cart" width="300" border="0" cellspacing="5" cellpadding="2">
<tr>
<td>
<label for="product">Product:</label>
</td>
<td>
<input name="product" required pattern="[a-zA-Z ]*$" title="Please enter only alphabetic characters" type="text" placeholder="Product name" size="28" />
</td>
</tr>
<tr>
<td>
<label for="quantity">Quantity:</label>
</td>
<td>
<input name="quantity" required type="number" title="Enter item quantity" placeholder="Product quantity" width="196px" />
</td>
</tr>
<tr>
<td>
<label for="price">Price:</label>
</td>
<td>
<input name="price" required pattern="[0-9]" title="Please enter only numeric characters" placeholder="Product price" size="28" />
</td>
</tr>
</table>
<br>
<div id="buttons">
<input type="reset" name="reset" id="resetbtn" class="resetbtn" value="Reset">
<input type="submit" name="submit" id="submitbtn" class="submitbtn" tabindex="7" value="Submit this!" onclick="">
<br style="clear:both;">
</div>
</form>
</section>
</section>
<table width="416" border="0" cellspacing="5" cellpadding="2" id="cart">
<tr>
<th width="115" scope="col">Products</th>
<th width="112" scope="col">Quantity</th>
<th width="92" scope="col">Price</th>
<th width="56"></th>
</tr>
<tr>
<td id="items">
</td>
</tr>
<tr>
<td scope="col"> </th>
<td scope="col">
</th>
<td scope="col"> </th>
<th>Total</th>
<td id="total">0</td>
</tr>
</table>
<script>
$(document).ready(function(){
var form = document.order;
var $checkout = $('#cart');
// Listen for form submit
$(form).on('submit', function(e){
// Prevent browser from sending form
e.preventDefault();
// this is a row thats nt yet attached to the document
var $row = $('<tr class="item">');
/*
* Loop through fields and add 'product','quantity','price'
* to $row. we store the data on the node as well
*/
$.each(['product','quantity','price'],function(index, key){
var $td = $('<td>');
var value = form[key].value;
$td.addClass(key).text(value);
$row.data(key, value);
$row.append($td);
});
// Attach the $row to the document
$('#items').append($row);
// Update the totals
$checkout.trigger('change');
});
// Update totals when cart changes
$checkout.on('change',function(){
var total = 0;
$(this).find('.item').each(function(){
var quant = parseFloat($(this).data('quantity'));
var price = parseFloat($(this).data('price'));
total = total + (quant * price);
});
$('#total').text(total);
});
});
</script>