Ways to calculate the total order amount when the quantity is modified

The order entry form includes columns for product name, price, and quantity:

 <table id="order-products" class="mobileorder-table">
        <colgroup>
            <col style="width: 80%;">
            <col style="width: 10%;">
            <col style="width: 10%;">
        </colgroup>

        <tbody>
                <tr>
                    <td>
                       Product1
                    </td>
                    <td>
 <span class="mobileorder-price">0,98</span>
                    </td>
                    <td>
                        <input data-product="4750211645618" class="quantity" id="product_Soodkogus" name="product.Soodkogus"
                       type="number" min="0" max="999999" value=""
                       onblur="orderSumRefresh()" />
                    </td>
                </tr>
        </tbody>
    </table>
    Order total <p id="js-doksumma"></p>

If the quantity is changed, the order total value should be updated. I attempted

<script>
    function parseFloatFormatted(txt) {
    if (typeof txt !== 'string' || txt === null || txt === "") {
      return 0
      }
    return parseFloat(txt.replace(',', '.').replace(' ', ''))
    }

function orderSumRefresh() {
    let totalAmount = 0
    const table = document.getElementById("order-products")
    table.rows.forEach((row) => {
       const hind = row.cells[1].querySelector(".mobileorder-price").innerText
       const kogus = row.cells[2].querySelector(".quantity").value
       const rowSum = Math.round(parseFloatFormatted(hind)* parseFloatFormatted(kogus) * 100) / 100
       totalAmount += rowSum
       });
    var dok = document.getElementById("js-doksumma")
    dok.innerText = totalAmount.toFixed(2)
    }

</script>

but encountered an error

What is the correct way to implement this? Should pure CSS, JavaScript, or jQuery be used?

A modern Chrome browser is being used on a mobile phone with an ASP.NET 6 MVC Razor application.

Answer №1

Nick Vu pointed out a problem in the for loop, so I made this adjustment:

for (let i = 0; i < table.rows.length; i++) {

Upon further investigation, I discovered more issues in the code. For instance, the index of the childNodes was incorrect. By using

console.log(row.cells[1].childNodes)

You can observe that there are 3 children and you are attempting to access the middle one (index: 1).

To retrieve data from the input element, make sure to utilize the .value property like so:

const quantity = row.cells[2].childNodes[1].value

********************* EDIT *******************

Modifying the code based on the updated solution.

To extract data from the html element, utilize the .innerHTML property.

function parseFloatFormatted(txt) {
    if (typeof txt !== 'string' || txt === null || txt === "") {
        return 0
    }
    return parseFloat(txt.replace(',', '.').replace(' ', ''))
}

function orderSumRefresh() {
    let totalAmount = 0
    const table = document.getElementById("order-products")
    /*
    for (let i = 0; i < table.rows.length; i++) {
        const row = table.rows[i]
        const price = row.cells[1].childNodes[1].innerHTML
        const quantity = row.cells[2].childNodes[1].value
        const rowSum = Math.round(parseFloatFormatted(price) * parseFloatFormatted(quantity) * 100) / 100
        totalAmount += rowSum
    }
    */
    for (const row of table.rows) {
        const price = row.cells[1].querySelector(".mobileorder-price").innerHTML
        const quantity = row.cells[2].querySelector(".quantity").value
        const rowSum = Math.round(parseFloatFormatted(price) * parseFloatFormatted(quantity) * 100) / 100
        totalAmount += rowSum
    }
    const totalPrice = document.getElementById("js-doksumma")
    totalPrice.innerText = totalAmount.toFixed(2)
}
<table id="order-products" class="mobileorder-table">
    <colgroup>
        <col style="width: 80%;">
        <col style="width: 10%;">
        <col style="width: 10%;">
    </colgroup>

    <tbody>
        <tr>
            <td>
                Product1
            </td>
            <td>
                <span class="mobileorder-price">0.98</span>
            </td>
            <td>
                <input data-product="4750211645618" class="quantity" id="product_Soodkogus" name="product.Soodkogus"
                    type="number" min="0" max="999999" value="" onblur="orderSumRefresh()" />
            </td>
        </tr>
    </tbody>
</table>
Order total <p id="js-doksumma"></p>

I recommend utilizing console.log() to debug and evaluate variables for any potential coding errors.

Answer №2

It is important to note that table.rows is not an array, but rather an HTMLCollection. To address this issue, you can easily rectify it by:

  const table = document.getElementById("order-products")
  for (const row of Array.from(table.rows)) {
  }

If you'd like to verify the presence of a "length" property being looped through, inspect the table in the elements tab of the dev tools and execute the following snippet in the console:

for (let i in $0.rows) {
    console.log(i);
    console.log($0.rows[i].cells[0]);
}

You will observe that during the final iteration, "length" is printed before triggering an exception.

Answer №3

Your issue starts here

for (let i in table.rows) {}

The values will be "0" and "length" (not the index as expected), which causes an error when trying to access row.cells[0].childNodes (row.cells is undefined)

I recommend updating it to

for (const row of table.rows) {}

The complete code would look like this:

function parseFloatFormatted(txt) {
  if (typeof txt !== 'string' || txt === null || txt === "") {
    return 0
  }
  return parseFloat(txt.replace(',', '.').replace(' ', ''))
}

function orderSumRefresh() {
  let totalAmount = 0
  const table = document.getElementById("order-products")
  for (const row of table.rows) {
    const price = row.cells[1].childNodes[0].innerHTML
    const quantity = row.cells[2].childNodes[0].innerText
    const rowTotal = Math.round(parseFloatFormatted(price) * parseFloatFormatted(quantity) * 100) / 100
    totalAmount += rowTotal
  }
  const docTotal = document.getElementById("js-doksumma")
  docTotal.innerText = totalAmount.toFixed(2)
}
<table id="order-products" class="mobileorder-table">
  <colgroup>
    <col style="width: 80%;">
    <col style="width: 10%;">
    <col style="width: 10%;">
  </colgroup>

  <tbody>
    <tr>
      <td>
        Product1
      </td>
      <td>
        <span class="mobileorder-price">0,98</span>
      </td>
      <td>
        <input data-product="4750211645618" class="quantity" id="product_Soodkogus" name="product.Soodkogus" type="number" min="0" max="999999" value="" onblur="orderSumRefresh()" />
      </td>
    </tr>
  </tbody>
</table>
Order total
<p id="js-doksumma"></p>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Trigger a function when the value of the file input field changes using ng-change

I'm attempting to trigger my upload() function when the file input changes, but I'm having trouble getting it to work. This is the HTML code: <input type="file" ng-model="image" ng-change="uploadImage()"> And here's the correspondin ...

Issues are arising with the for loop in an express node js app using ejs, as it is not displaying the intended data and

I am currently utilizing a for loop in JavaScript to display all the users from the database using ejs. I have included the code snippet below. This is within an express/node js application where SQL is used for data storage. <div class = "Contacts ...

Showing the `ViewBag` data within the `@Html.DropDownListFor` method enclosed

I'm currently working with a DropDownListFor that is set up like this: <div class="form-horizontal" id=CurrencyDataBlock> @Html.DropDownListFor(model => model.Code, ViewBag.Currency as SelectList, "--Select Currency--", n ...

JavaScript Numbers Having Strange Behavior

Insight The highest integer that can safely be stored in JavaScript is 9007199254740991 link Dilemma 1000000000000 === 999999999999.999999 // Returns true 1000000000000 === 999999999999.99999 // Returns true 1000000000000 === 999999999999.9999 // Re ...

Using jQuery that has been installed via npm within an Express application

I recently set up a node.js + express application and utilized npm to install jQuery. Within the app.js file, I included the following code: var jquery = require('jquery'); In the header of my html file, I have incorporated JavaScript that rel ...

Issue with Vue-Multiselect: Unselecting a group of pre-loaded values is not functioning as expected

My code: https://jsfiddle.net/bgarrison25/tndsmkq1/4/ Html: <div id="app"> <label class="typo__label">Groups</label> <multiselect v-model="value" :options="options" :multiple="true" group-values="libs" g ...

Track user engagement across multiple platforms

Looking for solutions to log system-wide user activity in my Electron app. I want to track mouse-clicks and keystrokes to determine if the user is inactive for a certain period of time, triggering a timer reset within the application. I believe I may nee ...

bootstrap 4 appears to encounter some responsiveness issues when it comes to rendering on iPhone

https://i.sstatic.net/rdOtX.png While inspecting my website on mobile using developer tools, everything looks perfect. However, when I view it on my actual iPhone 6, the layout seems off. Could this be a bug specific to the iPhone 6? Here are my meta tag ...

The Google Share button may fail to be displayed correctly if it was initially hidden

Is there a solution to the issue where the Google Share button does not display properly when it is initially hidden using CSS display:none on its parent div and then shown using jQuery .show()? I am currently experiencing this problem and I'm unsure ...

Angular: Identifier for Dropdown with Multiple Selection

I have recently set up a multi select dropdown with checkboxes by following the instructions provided in this post: https://github.com/NileshPatel17/ng-multiselect-dropdown This is how I implemented it: <div (mouseleave)="showDropDown = false" [class. ...

I need assistance with this ajax/javascript/php

I am currently working on creating a dynamic chained list. The idea is to have the user make selections from the first four dropdown lists, and based on their choices, a fifth dropdown list should appear. However, I am facing some issues with my code as th ...

Tips on connecting a file upload button to a flip card

In my current project located in index.html, I have integrated a flip card as a profile photo feature. Additionally, I have included a file uploader button on the page. However, I am struggling to connect the functionality of the file uploader button to au ...

Transforming a Bootstrap 4 dropdown navbar by replacing the caret with Fontawesome 5 icons for Plus and Minus

After spending several days trying to find a solution, I am still struggling. I have managed to create a responsive Navbar with Dropdowns using Bootstrap 4. Now, my goal is to replace the caret with a Plus and Minus sign at the end of each row on small dev ...

Problems Arising with HTML Form Functionality

After creating an HTML form, I encountered an issue where upon submission, it prompts me to open G Mail or Outlook in order to send the email. Although the correct email address is populated, I wish for the email to be sent without having to open any ext ...

Discovering a specific value within a JSON stringified array

I am looking for a specific value within a JSON stringify array [{"id":"432","temperature":"1","humidity":"1","createat":"0000-00-00 00:00:00"},{"id":"433","temperature":"22.00","humidity":"48","createat":"2015-10-11 19:49:57"},{"id":"434","temperature":" ...

What is the best way to interrupt an animation and restart it?

On my webpage, I have implemented some anchors and links that navigate to these anchors. When I click on a link, the background-color of the anchor changes. I use animation to gradually fade out this color over 10 seconds - starting with white and then rem ...

PHP file secured to only accept variables posted from HTML form

This is a basic HTML/AJAX/PHP script I have implemented. <form id="new_user" action="" method="post"> <div class="col-md-3 form-group"> <label for="username">Username</label> <input type="text" class="form-control" name ...

Developing a trivia game using HTML and JavaScript

I'm in need of some serious assistance with creating a quiz using HTML. My goal is to have a web page where users can visit, take a quiz, and have their responses stored. Unfortunately, I don't have the knowledge or skills required to do this on ...

Expiration Date of Third-Party Cookies

I need help retrieving the expiration date of a third-party cookie programmatically using JavaScript. Even though I can see the expiry time in the browser's DevTools (refer to the screenshot at https://i.sstatic.net/BW072.png), I am struggling to figu ...

Wrapping header text in Vuetify's v-data-table

Struggling with wrapping the header text on a v-data-table component. I've tried applying styles, but only tbody elements are affected. The header (thead element) remains unchanged. Any suggestions on how to get custom styling for the header? For ins ...