Change hidden field based on input field also multiplication... (cost calculator)

Discussion in 'JavaScript' started by emailarron, Oct 25, 2012.

  1. #1
    Hi guys, I am trying to create a Javascript price calculator based on the users input, plus I need it to do some math...

    So the price per item is $1.00 per 1000 units.

    So if the buyer enters in 1000, the calculator will change the amount hidden field to 1.00, and the cost div to 1.00 also.

    HTML:

    
    I want to buy <input type="text" class="fieldbox" style="width: 150px;" name="userinput"> items.
    
    Your total cost is: <div id="cost">($)</div>
    
    <input type="hidden" name="amount" value="AMOUNT BASED ON USER INPUT">
    
    HTML:
    I am very new to JS so any help would be great!
     
    emailarron, Oct 25, 2012 IP
  2. Unni krishnan

    Unni krishnan Peon

    Messages:
    237
    Likes Received:
    9
    Best Answers:
    2
    Trophy Points:
    0
    #2
    You can do something like the below code
    
    
    I want to buy <input type="text" class="fieldbox" style="width: 150px;" name="userinput" onKeyUp="performMath(this.value)"> items.
    Your total cost is: <div id="cost">($)</div>
    <input type="hidden" name="amount" id="amount" value="AMOUNT BASED ON USER INPUT">
    
    <script type="text/javascript">
    
    function performMath(value) {
        document.getElementById('cost').innerHTML= value/1000;
        document.getElementById('amount').value = value/1000;
        
    }
    
    </script>
    
    
    Code (markup):
     
    Unni krishnan, Oct 25, 2012 IP
  3. emailarron

    emailarron Member

    Messages:
    18
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    36
    #3
    Thank you! That worked perfectly.
     
    emailarron, Oct 29, 2012 IP
  4. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,999
    Best Answers:
    253
    Trophy Points:
    515
    #4
    If it's hidden, why are you calculating it client side?
     
    deathshadow, Nov 4, 2012 IP