Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New to the CF scene
    Join Date
    Sep 2011
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Angry Help with calculating shopping total with a quantity textbox

    I've been given this ridiculous assignment where I have to create a shopping web page. I really need help finding the javascript codes that will allow the user to update their total as they enter the quantities of the different products in the assigned text boxes.
    I don't even know where to start because we were never taught how to do this in class!
    Last edited by AnnaKaren; 09-27-2011 at 02:41 PM.

  • #2
    Regular Coder
    Join Date
    Sep 2011
    Location
    Sweden
    Posts
    154
    Thanks
    1
    Thanked 22 Times in 22 Posts
    Seems like a fun assignment to me...

    Start with thinking about the logic of a shopping cart.
    What does it need to be able to do (add, delete different items, different quantitites etc.). Continue to think about this until you think you know all the things it will have to be able to do. Then come back here and explain this in a clear fashion and hopefully you will get more help.

  • #3
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,252
    Thanks
    203
    Thanked 2,557 Times in 2,535 Posts
    Quote Originally Posted by AnnaKaren View Post
    I've been given this ridiculous assignment where I have to create a shopping web page. I really need help finding the javascript codes that will allow the user to update their total as they enter the quantities of the different products in the assigned text boxes.
    I don't even know where to start because we were never taught how to do this in class!
    I do not see how anyone can help you very much here. Complain to your tutor.


    This will give you a skeleton:-


    Code:
    <form name = "myform">
    PRODUCT 1 QTY <input type = "text" name = "p1" onblur = "makeTotal()"><br>
    PRODUCT 2 QTY <input type = "text" name = "p2" onblur = "makeTotal()"><br>
    TOTAL COST <input type = "text"  name = "tot" readonly>
    </form>
    
    <script type = "text/javascript">
    function makeTotal() {
    var a = Number(document.myform.p1.value);
    var b = Number(document.myform.p2.value);
    // you must check that the input values are positive integer numbers!
    var t = a*1.20 + b *2.50;  // multiply quantities by unit price
    document.myform.tot.value = t.toFixed(2);  // display total to 2 decimal places
    <script>
    “A man ceases to be a beginner in any given science and becomes a master in that science when he has learned that he is going to be a beginner all his life.” Robin G. Collingwood (English Philosopher, 1889-1943)

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #4
    New to the CF scene
    Join Date
    Sep 2011
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    We are not required to make the check-out table completely interactive.
    I have made mine so that the product names, codes and prices are already in the table and the user would just have to enter the quantities into a text box.

    I'm just really struggling to find javascript codes that will allow the totals to accumulate the at the end of the rows as numbers are entered into the quantity box and also give an overall total at the bottom of the table.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •