function output as form value

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • themightystef
    New Member
    • Apr 2012
    • 3

    function output as form value

    hi,

    i've been looking for the right answer but can't seem to find it, so I figured i'd give this a try.

    I'm trying to make a website where visitors can input the quantity of a certain product they want, then press a button to have a function calculate the total price of the selected products. so far I've ovly been able to get this final value in an alert box, but what I'm trying to do is to have the final price written in a textfield next to the function add button, mostly because the order eventually has to be sent to the company by mail and they would like to have the final price included.

    I hope i was clear enough, if not, please comment and i'll try to make it a bit clearer(I'm 17 and english is not my native language)

    thanks in advance
    Stef
  • acoder
    Recognized Expert MVP
    • Nov 2006
    • 16032

    #2
    You need to access the text box and set its value, e.g.
    Code:
    document.getElementById("text_box_id").value = total;

    Comment

    • themightystef
      New Member
      • Apr 2012
      • 3

      #3
      I think i was too unclear:P I meant to have the final amount to be entered into the textbox after the visitor presses the button. I'll try to simplify the code a bit(coz the total code is 161 lines...)
      Code:
      <html>
      <head>
      <script>
      function add(){
      var q1=document.myForm.productname.value;
      t1=q1*15.30;
      total=t1
      alert("€ "+total);}
      </script>
      </head>
      <body>
      <form name="myForm">
      <input type="text" name="productname">
      <input type="text"name="total">
      <input type="button" onclick="add()" value="total">
      </form>
      </body>
      </html>
      so what i want to do is have the total to be written in the last text box named total(maybe your code was right but i put it in the wrong spot, i just put it as last line of the add function now)

      thanks

      Comment

      • themightystef
        New Member
        • Apr 2012
        • 3

        #4
        nevermind, i forgot to add the id for the textbox, but its working now, thank you very much for your help(:

        Comment

        Working...