Results 1 to 2 of 2

Thread: Get price via quantity field

  1. #1
    Join Date
    Mar 2005
    Location
    Western Australia
    Posts
    148
    Thanks
    24
    Thanked 4 Times in 4 Posts

    Default Get price via quantity field

    Hi guys, I am using a script that was provided to me for changing an image based on the selection of several selects (http://www.dynamicdrive.com/forums/s...ad.php?t=59873)

    However now what I am trying to do is set a value for quantity and have that change the result if the quantity changes. My modified code looks like;

    Code:
    function mpdprice(){
     var mpprice = '1';
     var string = document.getElementById('quantity-input').value;
     if (string == '1'){
      mpprice='22.80';
     }
     else if (string >= '2' && string <= '24' ){
      mpprice='22.08';
     }
     else if (string >= '25' && string <= '49' ){
      mpprice='21.36';
     }
     else if (string >= '50' && string <= '99' ){
      mpprice='19.93';
     }
     else if (string >= '100' && string <= '249' ){
      mpprice='18.49';
     }
     else if (string >= '250'){
      mpprice='17.06';
     } 
     document.getElementById('matprice').innerHTML='$'+mpprice+' each';
    }
    and the HTML for the price value and that field is

    HTML Code:
    <span id="matprice">$22.80 each</span>
    <input type="text" maxlength="5" value="1" id="quantity-input" onchange="mpdprice();">
    While it appears to work some values are not correct, if you test it you will see for example setting a quantity to 10 gives $1.00????

    Please help me fix the code.

    Thanks

    GW
    1st rule of web development - use Firefox and Firebug
    2nd rule - see the first rule
    --
    I like Smilies

  2. #2
    Join Date
    Dec 2008
    Location
    Portsmouth, UK
    Posts
    1,891
    Thanks
    2
    Thanked 441 Times in 435 Posts

    Default

    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
    
    <head>
      <title></title>
    <script  type="text/javascript">
    /*<![CDATA[*/
    function mpdprice(){
     var mpprice = '1';
     var string = document.getElementById('quantity-input').value.replace(/\D/g,''); // remove all none digits
     if (string){
      if (string == '1'){
       mpprice='22.80';
      }
      else if (string <= 24 ){
       mpprice='22.08';
      }
      else if (string <= 49 ){
       mpprice='21.36';
      }
      else if (string <= 99 ){
       mpprice='19.93';
      }
      else if (string <= 249 ){
       mpprice='18.49';
      }
      else if (string >= 250){
       mpprice='17.06';
      }
     }
     document.getElementById('matprice').innerHTML='$'+mpprice+' each';
    }/*]]>*/
    </script></head>
    
    <body>
    <span id="matprice">$22.80 each</span>
    <input type="text" maxlength="5" value="1" id="quantity-input" onchange="mpdprice();">
    </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. The Following User Says Thank You to vwphillips For This Useful Post:

    gwmbox (01-07-2011)

Bookmarks

Posting Permissions

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