The total calculations for each row in Javascript and Ajax are experiencing glitches and are not functioning correctly

Below is the script utilized by an AJAX library and JavaScript.

The total order column for total price is functioning correctly. However, the total calculations for the discount column and net price column are not working as expected.

<script type="text/javascript">
$(document).ready(function() {
    $('.row1').keyup(function(ev){

        var row1c = $(this).val() * 25;
        $('.row1c').html((row1c).toFixed(2));

    });
    $('.row1a').keyup(function(ev){

        var row1ac = $(this).val() * 15;
        $('.row1ac').html((row1ac).toFixed(2));

    });
    // Additional javascript functions...
});


</script>

<table border="2" cellpadding="5" cellspacing="2" align="center">


  <tr>
    <th>Cracker Name</th>
    <th>Cracker Photo</th>
    <th>Packing Model</th>
    <th>Retail Price</th>
    <th>You Save (Discount - 60%) <br /></th>

    <th>Selling Price<br /></th>

    <th>Quantity</th>
    <th>Total Order</th>
    <th>Discount</th>
    <th>Net Price</th>
  </tr>


  <tr>
    <td>3.5'' Laxmi</td>
    <td>--</td>
    <td>5 in 1 Packet</td>
    <td>25</td>
    <td>15</td>
    <td>10</td>
    <td>
      <input type="text" name="pages" class="row1 row1a row1b" />    </td>
    <td><span class="row1c">0.00</span>    </td>
      <td><span class="row1ac">0.00</span>    </td>
        <td><span class="row1bc">0.00</span>    </td>
  </tr>
  <tr>
    <td>3.5'' Laxmi</td>
    <td>--</td>
    <td>5 in 1 Packet</td>
    <td>45</td>
    <td>25</td>
    <td>20</td>
    <td>
      <input type="text" name="pages" class="row2 row2a row2b" />    </td>
    <td><span class="row2c">0.00</span>    </td>
      <td><span class="row2ac">0.00</span>    </td>
        <td><span class="row2bc">0.00</span>    </td>
  </tr>






  <tr>
    <td colspan="7" align="right">Total  </td>

    <td><span class="row5c">0.00</span>    </td>
     <td><span class="row55c">0.00</span>    </td>
      <td><span class="row555c">0.00</span>    </td>
  </tr>
</table>

Answer №1

Update to your SCRIPT with the code provided below.

$(document).ready(function() {
    $('.row1').keyup(function(ev){

        var row1c = $(this).val() * 25;
        $('.row1c').html((row1c).toFixed(2));

    });
    $('.row1a').keyup(function(ev){

        var row1ac = $(this).val() * 15;
        $('.row1ac').html((row1ac).toFixed(2));

    });
    $('.row1b').keyup(function(ev){

        var row1bc = $(this).val() * 10;
        $('.row1bc').html((row1bc).toFixed(2));

    });

   $('.row2').keyup(function(ev){

        var row2c = $(this).val() * 45;
        $('.row2c').html((row2c).toFixed(2));

    });
    $('.row2a').keyup(function(ev){

        var row2ac = $(this).val() * 25;
        $('.row2ac').html((row2ac).toFixed(2));

    });
    $('.row2b').keyup(function(ev){

        var row2bc = $(this).val() * 20;
        $('.row2bc').html((row2bc).toFixed(2));

    });
  $('input[type=text]').keyup(function(){
    var total = 0;
    $('input[type=text]').each(function(){
      total += Number($(this).parent().next().find('span').text());
    })
    $('.row5c').text(total.toFixed(2));
  })


  $('input[type=text]').keyup(function(){
    var total = 0;
    $('input[type=text]').each(function(){
      total += Number($(this).parent().next().next().find('span').text());
    })
    $('.row55c').text(total.toFixed(2));
  })
   $('input[type=text]').keyup(function(){
    var total = 0;
    $('input[type=text]').each(function(){
      total += Number($(this).parent().next().next().next().find('span').text());
    })
    $('.row555c').text(total.toFixed(2));
  })
});

Updated two lines for discounts:

total += Number($(this).parent().next().next().find('span').text());

and for the net total:

Number($(this).parent().next().next().next().find('span').text());

DEMO

Answer №2

Add custom classes to your HTML elements like so :

<td><input type="text" name="pages" class="custom1 custom1a custom1b" /></td>
<td><span class="custom1c total1">0.00</span></td>
<td><span class="custom1ac total2">0.00</span></td>
<td><span class="custom1bc tota3">0.00</span></td>

Next, include the following :

<td><input type="text" name="pages" class="custom2 custom2a custom2b" /></td>
<td><span class="custom2c total1">0.00</span></td>
<td><span class="custom2ac total2">0.00</span></td>
<td><span class="custom2bc tota3">0.00</span></td>

Here are some sample functions for totaling:

$('input[type=text]').keyup(function() {
    var total = 0;
    $('.total1').each(function() {
        total += Number($(this).text());
    })
    $('.row5c').text(total.toFixed(2));
});

$('input[type=text]').keyup(function() {
    var total = 0;
    $('.total2').each(function() {
        total += Number($(this).text());
    })
    $('.row55c').text(total.toFixed(2));
});
$('input[type=text]').keyup(function() {
    var total = 0;
    $('total3').each(function() {
        total += Number($(this).text());
    })
    $('.row555c').text(total.toFixed(2));
});

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

What is the best way to ensure an inline-block element spans the entire width?

Is there a way to make an inline block fill the entire width without using !important in AMP? I have tried making all fields fit the whole width but it's not working. How can I achieve this without resorting to !important? Below is the code I am curre ...

Meteor JS failed to load the CSS file due to an unsupported MIME type

Currently, I am working on a meteor js 1.8 app. I have created a layout and now I want to add CSS to it. Here is the structure I am following: imports -ui --stylesheets --bootstrap.min.css --font-awesome.min.css --skins.css --forms.css All my CSS files ...

Tips for exporting a React Component without using ownProps in a redux setup with TypeScript

I'm currently working on a project using typescript with react-redux. In one of my components, I am not receiving any "OwnProp" from the parent component but instead, I need to access a prop from the redux state. The Parent Component is throwing an er ...

I have successfully deployed a basic nodejs application using Heroku, but encountering an error when attempting to open the URL

Accidentally, I mistakenly deployed two apps using the same repository, assuming there was an error in the deployment process. I have never deployed any app using Heroku before, so please investigate my issue. Here is my index.js file: const express = req ...

Preserving state values with the useState hook throughout various function invocations

When I click the button to delete department rows from my table, I am using the "deleteDepartment" function. The issue arises when trying to get the selected row index in the "selectedRows" hook. It seems that the "rowSelection" state keeps accumulating va ...

There is no Api.js file in the Cordova iOS platform

I have been attempting to integrate the iOS platform into a new Cordova 7.0.1 project, but I continuously encounter the error mentioned in the title of this post. Despite following the steps outlined in this thread here, including removing and re-adding ...

how to check if a string has white spaces in a react application

If the input string has white space, the alert will indicate unsuccessful. If the input string does not have any white space, the alert will be successful. import React from "react"; export default function DropDown() { let i = document.getEle ...

Exporting a variable that is currently in use within a function being exported

I am currently working on setting up my mongodb in a separate file. I have created an initialization function where I write all the necessary code for connecting to the database and exporting the function. However, I also need to export the 'db' ...

Changing the color variable of an object using an onClick function in JavaScript

I'm currently working on a simple game where users can draw using the keys W, A, S, and D. If you want to take a look at the progress I've made so far, here is a JSFiddle link. There's a collision function in the code that I no longer need, ...

Implementing a feature that allows users to initiate a download window in a node.js express application

I have a node.js + express application in development. A scenario I am facing involves creating an excel file on the server side and then serving it to the user for download, allowing them to choose their preferred directory. Once the file is downloaded, I ...

Navigate to error page from AJAX request in ASP.NET MVC

If an error is returned from an AJAX call in a MVC application, how can you redirect to the error page Error.cshtml located in the Shared folder? $.ajax({ //Some stuff here... error: function (jqXHR, textStatus, errorThrown) { window.loc ...

What are some steps I can take to diagnose why my Express server is not receiving POST requests from my HTML form?

Struggling with an unexpected issue on my website where the form submission is not triggering the POST request to my Express server. I've set up a MongoDB database and created a HTML form to store user data, but something seems to be amiss. HTML: & ...

Transform data from API arrays into a remapped object with updated keys and structure

I received the following output from my return object: { data: [ { ts: "20-10-26", events: 1500, }, { ts: "20-10-27", events: 1280, }, { ts: "20-10-28", events: 1111, } ] } T ...

"Two columns of identical width, each occupying 50% of the screen height

I am currently working on a design featuring two columns, each set to occupy 50% of the width. I am facing a challenge with ensuring that the height of these columns stretches to the full height of the screen, even when they are empty, so I can showcase th ...

Unable to switch between navigation items and icons

Currently, I am in the process of learning vanilla JavaScript and I'm trying to incorporate a navigation feature similar to when the hamburger icon is clicked. However, I have encountered an issue where I cannot toggle the hamburger icon to change it ...

Having trouble with Google Maps Places API autocomplete feature; it's not functioning properly

My goal is to integrate the Google Maps Places API with a search box. To test jQuery autocomplete, I managed to make it work using this simple example: function bindAutocomplete() { var locationSearch = $("input#Location"); locationSearch.autocom ...

Can CSS be used for creating unique color combinations?

I am facing a challenge where I have two div elements with different transparent, colored backgrounds that overlap each other. My goal is to find a way to customize the color in the area where these elements overlap. For instance, if I blend red and blue ...

The navigation/hamburger icon vanishes after a click on a page link

I'm in the process of creating a single-page scrolling website. However, I am encountering an issue with the navigation bar. Specifically, when I click on a page link from the nav bar at screen widths less than 780px (when the hamburger icon appears), ...

Building multiple files in React allows developers to divide their code

Can a React/Redux application be split into modules during webpack build? For example, having a set of components for users and another set for invoices. I want webpack to generate users.js and invoices.js that can be imported into index.html. If I make ch ...

Scroll automatically to the following div after a brief break

On my website, the title of each page fills the entire screen before the content appears when you scroll down. I am interested in adding a smooth automatic scroll feature that will transition to the next section after about half a second on the initial "ti ...