www.webdeveloper.com
Results 1 to 4 of 4

Thread: Calculating Price from Quantity selected by the user

Hybrid View

  1. #1
    Join Date
    Aug 2013
    Posts
    2

    Calculating Price from Quantity selected by the user

    ok so i have this form on my web page.

    HTML Code:
    <div id="purhcaseForm">
    	 <form id="purchase">
    		 <br> Item Price: $463.50
    		 <br> Please Select Quantity
    		 <input type='button' name='subtract' onclick='javascript: document.getElementById("qty").value--;' value='-'/>
    		 <input type='button' name='add' onclick='javascript: document.getElementById("qty").value++;' value='+'/>
    		 <input type='text' name='qty' id='qty' />
    what i want to do is in the simplest way possible is to have another box which outputs the total order price taking into account the quantity the user has specified. im very new to java script and do not have any clue as to where to start ( i dont even know if what i have is the best way of achieving my objective) all help is apreciated

  2. #2
    Join Date
    Jun 2004
    Location
    Portsmouth UK
    Posts
    2,683
    Code:
    <!DOCTYPE html>
    <html lang="en">
    <head>
      <meta charset="utf-8" />
      <script>
    function Total(qty,ud,total,value){
     qty=document.getElementById(qty);
     ud>0?qty.value++:qty.value--;
     qty.value=Math.max(qty.value,0);
     document.getElementById(total).value=qty.value*value;
    }
      </script>
    </head>
    <body>
        <div class="slider-vertical"></div>
    <div id="purhcaseForm">
    	 <form id="purchase">
    		 <br> Item Price: $463.50
    		 <br> Please Select Quantity
    		 <input type='button' name='subtract' onclick='Total("qty",-1,"total",463.50);' value='-'/>
    		 <input type='button' name='add' onclick='Total("qty",1,"total",463.50);' value='+'/>
    		 <input type='text' name='qty' id='qty' readonly=true value="0"/>
    		 <input type='text' name='total' id='total' value="0" />
          </form>
    </div>
             </body>
    </html>
    Vic

    God loves you and will never love you less.

    http://www.vicsjavascripts.org/Home.htm
    If my post has been useful please donate to http://www.operationsmile.org.uk/

  3. #3
    Join Date
    Aug 2013
    Posts
    2

    Re: Calculating Price from Quantity selected by the user

    Hi, thats exactly what i wanted. if its to much trouble dw about it but can you briefly explain what this part of the code does?

    HTML Code:
    <script>
    function Total(qty,ud,total,value){
     qty=document.getElementById(qty);
     ud>0?qty.value++:qty.value--;
     qty.value=Math.max(qty.value,0);
     document.getElementById(total).value=qty.value*value;
    }
      </script>





    I want to get some sort of basic understanding as to what you did. Thanks apreciate all your help

  4. #4
    Join Date
    May 2006
    Location
    Somewhere behind your screen
    Posts
    1,648
    Vic, i can't believe my eyes you're using inline onclicks
    xxx: Guess Buddhist riddle: "What is the sound of one hand clapping?"
    yyy: facepalm

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

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



Recent Articles