Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8998109
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T23:58:48+00:00 2026-06-15T23:58:48+00:00

I am creating a simple shopping cart prototype and want to make a JavaScript

  • 0

I am creating a simple shopping cart prototype and want to make a JavaScript array that will take the quantity of each table row, multiply it by the price, and display the total in the final column. The quantity value is a form where the user inputs the quantity. I would also like to include a table row with the grand total. I have these two functions but I am wondering how I can make one function work for the entire table:

<script type="application/javascript">
 function total()
 {
var qty = document.forms["qty"]["amount"].value;
var sum1 = qty * 5.99;
document.getElementById("total1").innerHTML="$" + sum1;
 }
</script>

<script type="application/javascript">
 function total2()
 {
var qty2 = document.forms["qty2"]["amount2"].value;
var sum2 = qty2 * 105.99;
document.getElementById("total2").innerHTML="$" + sum2;
 }
</script>

The table

<table id="cart">
<tr>
<th><b>Quantity</b></th>
<th><b>Item</b></th>
<th>Price</b></th>
<th><b>Total</b></th>          
</tr> 
<tr>
<td><form name="qty"><input type="number" name="amount" size="1" value="1" onchange="total1()"/></form></td>
<td>Product 1</td>
<td>$5.99</td>
<td id="total1"></td>
</tr>
<tr>
<td><form name="qty2"><input type="number" name="amount2" size="1" value="1" onchange="total1()"/></form></td>
<td>Product 2</td>
<td>$105.99</td>
<td id="total2"></td>
</tr>
<tr>
<td><b>Grand Total<b></td>
<td id="grand"></td>
</tr>
</table>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T23:58:48+00:00Added an answer on June 15, 2026 at 11:58 pm

    Remove the inline onchange event from both number type inputs and paste following code inside head tags between script tags

    (function grandTotal()
    {
        var cart=document.getElementById('cart'),
        forms=cart.getElementsByTagName('form'), total=0;
        for(var i=0;i<forms.length;i++)
        {
            var amount=forms[i].getElementsByTagName('input')[0];
            var qty=amount.value;
            var tr=forms[i].parentNode.parentNode;
            var price=tr.getElementsByTagName('td')[2].innerHTML.substr(1);
            total+=parseFloat(qty)*parseFloat(price);
            amount.onchange=grandTotal;
        }
        document.getElementById('grand').innerHTML=total.toFixed(2);
    })();
    

    Example Here.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm creating a simple Java wrapper for git executable, that I want to use
I'm creating simple image viewer, but I want to make a sorting of pictures
Im creating a simple WCF service for receiving crash reports. The service will run
Creating simple php login scripts is easy, with simple one table mysql integration. I
I'm creating simple Socket server. Flex application will be client for this server. On
I am creating simple blog web application using play framework and i want to
I'm creating simple form validation that excludes any entries that have a letter in
I am creating simple pojo webservice like TemperatureConversion. I was able to make deploy
Here is a simple example: Assume I have a customer table that has a
I'm creating simple payment form that allows users to select from several different gateways

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.