Calculating total price by iterating in the table using JavaScript

Calculating total price by iterating in the table using JavaScript

By : user2950083
Date : November 17 2020, 11:52 AM
it should still fix some issue I'm working on an exercise where we have to build a shopping cart using HTML and JavaScript.
code :
function calculate(){
var table=document.getElementById('shopTable');
var count = table.getElementsByTagName('tr').length;
if (count > 0)
    var total = 0;
    for(var i = 1; i < count; i++)
        total += table.rows[i].cells[1].innerHTML * table.rows[i].cells[2].children[0].value;


Share : facebook icon twitter icon
Calculating total price php

Calculating total price php

By : SilNak
Date : March 29 2020, 07:55 AM
wish of those help i hope you want this
first set the variable $total_products and $total_price
code :
$total_products = 0; // set to 0 initially
$total_price = 0; // set to 0 initially

    $count = 0;
        foreach ($products as $key => $product){
          $number = isset($_POST[$key])?$_POST[$key]:'';
                if (!$number){
                echo "";
                } else {
            echo "<tr>";
                echo "<td>";
                    echo $product;
                echo "</td>";
                echo "<td>";
                    echo $number;
                echo "</td>";
                echo "<td>";
                    $price = $prices[$count]* $number;
                    echo "&#8364 ", $price;
                echo "</td>";
            echo "</tr>";

            $total_products += 1; // update total_products +1
            $total_price += $price;  // update total_price +$price
echo "<tr>";
    echo "<td>";
        echo "Totaal";
    echo "</td>";
    echo "<td>";
        echo $total_products;
    echo "</td>";
    echo "<td>";
        echo $total_price;
    echo "</td>";
echo "</tr>";
Calculating total price using javascript

Calculating total price using javascript

By : TheBlueSky
Date : March 29 2020, 07:55 AM
To fix this issue You need a method with some kind of a loop over all your input fields, just like the one you created for one field, and trigger the call each time one field is updated:
code :
function getTotalPrice() {
  var total = 0,
      inputs = document.getElementsByTagName('input');
  for (var i = 0; i < inputs.length; i++) {
    if(inputs[i].value) {
      total += parseFloat(inputs[i].getAttribute("data-unitPrice")) * 
  if(total > 0) {
    document.getElementById('TARGET').innerText = '\u20ac ' + total.toFixed(2);
How to display both of total order sale price and regular price (or total discount price) in cart page in woocommerce

How to display both of total order sale price and regular price (or total discount price) in cart page in woocommerce

Date : March 29 2020, 07:55 AM
Does that help I found the answer. The answer is in the link below
Display Total Discount Amount / Total Savings
calculating (length*qty = total(m), total(m)*price = total price) but the total price wont show results

calculating (length*qty = total(m), total(m)*price = total price) but the total price wont show results

By : user3630208
Date : March 29 2020, 07:55 AM
may help you . From what I can tell from your code snippets, you want to populate the total fields. I made a minimal reproducible example based off of your code. I added id attributes to the input tags and I added a calculate button which does the math to calculate the totals for the fields. Hopefully this can get you on the correct path for you to just take what I have here and apply it to your code.
code :
$(document).on('click','#calc', function(){
	var qty2 = $('#qty2').val();
  var length = $('#length').val();
  var temp = qty2 * length;
  var qty = $('#qty').val(temp).val();
  var price = $('#price').val();
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table id ='tab_logic'>
        <th class="text-center"> # </th>
        <th class="text-center"> Product </th>
        <th class="text-center"> Width </th>
        <th class="text-center"> Length </th>
        <th class="text-center"> Qty </th>
        <th class="text-center"> Total(m) </th>
        <th class="text-center"> Price Rate </th>
        <th class="text-center"> Total Price </th>
      <tr id='addr0'>
        <td><input type="text" name='product[]'  placeholder='Enter Product Name' class="form-control"/></td>
        <td><input type="text" name='width[]'  placeholder='Enter Product Name' class="form-control"/></td>
        <td><input id="length" type="number" name='length[]'  placeholder='Enter Product Name' class="form-control"/></td>
        <td><input id="qty2" type="number" name='qty2[]'  placeholder='Enter Product Name' class="form-control"/></td>
        <td><input id ="qty" type="number" name='qty[]' placeholder='Enter Qty' class="form-control" step="0" min="0"/></td>
        <td><input id="price" type="number" name='price[]' placeholder='Enter Unit Price' class="form-control" step="0.00" min="0"/></td>
        <td><input id="totalNum" type="number" name='total[]' placeholder='0.00' class="form-control total" readonly/></td>
      <button id="calc">Calculate</button>
Calculating the total price

Calculating the total price

By : George Cook
Date : March 29 2020, 07:55 AM
Hope this helps Try if - if instead of if-else if.
if - else if condition is satisfied, the appropriate statements are executed and the remaining conditions are not evaluated.
Related Posts Related Posts :
  • How to change add and remove active class in JavaScript
  • how to toggle a specific div in javascript
  • How to get all the values from multiple keys of an array of object?
  • jQuery bind() unbind() and on() and off()
  • regular expression to find DD Month YYYY from a string
  • How to upload files from web client (HTML/jQuery form) to your Dropbox folder
  • Backbone history and require.js issue
  • Confusion over Classical and Prototypal Inheritance Javascript
  • How to take screen shot of current webpage using Javascript/JQuery?
  • Store meridiem in a variable
  • class declared inside closure vs standard class without closure
  • javascript confirm box always return true
  • dynamically changing the size of font size based on text length using css and html
  • Node.js - Getting the host IP address while on Amazon EC2
  • Confusing CSS, can anyone explain please
  • How to install v8-profiler on Windows 8 (64 bit) using npm?
  • Resolve promise based on another promise
  • google maps adding overlay layer above UI and markers
  • Style specific letter?
  • RangeError: Maximum call stack size exceeded with array.slice
  • node rest client get with local variable
  • If condition not executes while mouse move fastly
  • Count the number of displayed elements in a HTML list
  • Locate JavaScript source code in Emacs
  • Clean, Modular Code vs MV* Frameworks
  • infinite scroll without loading image
  • Backbone: reverse collection order with comparator
  • What do printers ignore?
  • jQuery UI .tabs() Contentless tab?
  • Execute Javascript alert() after page (visually) loaded
  • JavaScript - duplicating array doesn't work
  • Excessive clickable area below image
  • JavaScript Regex: Replace |b| with <b>
  • Unexpected value change in 2D array in JavaScript
  • Function doesnt see parameter as a array of objects
  • jQuery fetch keypress event on chrome and IE
  • How to enable jquery validate localization?
  • Cassandra map collection returned by node.js Helenus looks odd
  • angular ng-repeat with multiple filter options
  • Selecting Children without ID or Class names
  • How to uncheck a group of checkboxes when another checkbox is checked
  • Is hiding content by Javascript or jQuery Worth trying
  • Load XUL resource using javascript
  • XML to HTML text area from server file system
  • set focus() on textbox on form onload
  • es lint '' is assigned a value but never used' eventhough i have used it
  • Can "name" attribute be used for custom VueJS components
  • Get innerHTML of content when it is clicked
  • HTTPS causes CSS animations to not load? Very confused
  • How to hide other div apart from touched div to the band
  • Is global variable assignment atomic on NodeJS?
  • Datepicker onchangemonthyear beforeshowday
  • trying to render html files using jade but it still adresses it as a jade
  • Google Analytics - Understanding and Dissecting the Snippet: What is | | [ ];
  • Replacing "[aA09.b]." to "[aA09.b]\n" in in JavaScript
  • Why wrap a function definition in an immediate function?
  • Enable / disable jQuery script on switching to different tab in a HTML page
  • how can find return variable value outside anonymous function in node js mysql query function
  • Converting 1 to 0001 in javascript array
  • svg viewbox should not resize the text fontSize?
  • shadow
    Privacy Policy - Terms - Contact Us © ourworld-yourmove.org