I've created a dynamic HTML table where the sales man is ordering the customer order. This function is called on the click event of the Add Item Button.
var globalTID; // clear this global variable upon sale button.
function loadAddedData() {
$("#cart-table td").remove();
//var tid = $("#token").val();
alert("this is global Token ID :" + globalTID);
//alert(v1);
$.ajax({
url: "show-added-token.php",
data: {tokenID: globalTID},
type: "POST",
dataType: "JSON",
success: function(data) {
$.each(data, function(key, value) {
//$("#showme").append(value.item_id + "<br>" + value.item_unit_price + "<br" + value.order_qty);
$("#cart-table").append('<tr><td>' + value.item_name + '</td>' + '<td>' + value.item_unit_price + '</td>' + '<td>' + value.order_qty + '</td>' + '<td class="op">' + value.order_price + '</td>' + '<td>' + value.order_of + '</td>' + '<td>' + value.order_instructions + '</td>' + '<td>' + value.token_id + '</td>' + '<td>' + "<button class='btn-edit' data-eid=" + [value.cart_id] + ">Edit</button>" + '</td>' + '<td>' + "<button class='btn-delete' data-did=" + [value.cart_id] + ">Delete</button>" + '</td></tr>');
console.log(data);
});
}
});
};
What I want is to calculate the each order price upon the click event of the Calculate button. Here the .op is the class name which i applied on the of the order_price. I've seen different scenarios here at STACK but not fully understanding them.
var price = 0;
$('#cart-table tr').each(function() {
var tp = $(this).find(".op").text();
price = price + parseInt(tp);
/*if(jQuery.type(price) === "number"){
alert('its number');
}else{
alert('error');
}*/
});
console.log("total price is : " + price);
$("#tp").val(price);
But its showing NaN in the textbox (tp). Then I tried another one from the STACK and it also did not bring the desired output.
var tp = $(".op").text();
price += parseFloat(tp);
alert(price + "<br>");
$("#tp").val(price);