onclick javascript event not working

Discussion in 'JavaScript' started by tjkuster, Oct 31, 2007.

  1. #1
    I've published an order page where I'm trying to have totals calculated on a button onclick event.

    Here is the code in the <head>:

    <script type="text/javascript">
    function total() {
    var a=document.form1.blanketprice.value * "1";
    var b=document.form1.namefee.value * "1";
    var c=document.form1.dollprice.value * "1";
    var d=document.form1.giftwrapprice.value * "1";
    var totalcost= (a + b + c + d);

    document.form1.total.value = totalcost;
    }
    </script>



    Here is the code in the <body>:

    <td align="right" valign="middle" class="style2">Blanket Price: </td>
    <td colspan="3" align="left" valign="middle" class="style2"><input name="blanketprice" type="text" id="blanketprice" size="40"></td>
    </tr>
    <tr>
    <td align="right" valign="middle" class="style2">Embroidered Name Fee: </td>
    <td colspan="3" align="left" valign="middle" class="style2"><input name="namefee" type="text" id="namefee" size="40"></td>
    </tr>
    <tr>
    <td align="right" valign="middle" class="style2">Doll Price: </td>
    <td colspan="3" align="left" valign="middle" class="style2"><input name="dollprice" type="text" id="dollprice" size="40"></td>
    </tr>
    <tr>
    <td align="right" valign="middle"><span class="style2">Gift Wrap Price: </span></td>
    <td colspan="3" align="left" valign="middle"><span class="style2">
    <input name="giftwrapprice" type="text" id="giftwrapprice" size="40">
    </span></td>
    </tr>
    <tr>
    <td align="right" valign="middle"><strong>Order Total:</strong></td>
    <td colspan="3" align="left" valign="middle"><span class="style2">
    <input name="total" type="text" id="total" size="40">
    </span></td>
    </tr>
    <tr>
    <td width="33%" align="center" valign="middle">&nbsp;</td>
    <td colspan="3" align="left" valign="middle"><input name="UpdateTotal" type="button" class="style7" id="UpdateTotal" onClick="total();" value="Update Total"></td>
    </tr>


    Here is the link url to the entire order form:
    http://www.branchouttemp2.biz/createyourblanket.php
     
    tjkuster, Oct 31, 2007 IP
  2. e96

    e96 Active Member

    Messages:
    299
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    80
    #2
    What output are you getting?

    From a quick glance, you are converting numbers to strings every time that you
    multiply something by "1"
    Try cutting out the quotes from each
    IF that doesn't work try to see what your variables are storing with an alert box. For instance aler(document.form1.giftwrapprice.value ) will let you see what the value is.
     
    e96, Oct 31, 2007 IP
  3. -NB-

    -NB- Peon

    Messages:
    153
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Try wrapping this around each math statement:
    parseInt(integer * integer)
    Code (markup):
     
    -NB-, Oct 31, 2007 IP
  4. tjkuster

    tjkuster Guest

    Messages:
    3
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Thanks for the ideas!

    Tried both -- Also, I couldn't get the aler() to show me anything (maybe I'm an idiot)

    Still not working.
    When I click the "update total" button, I get no output at all in the totalcost field.

    What I do get is an additional error on the page that says "The object does not support this method." assumably referring to the button. How does a button not support an onclick?? ARG.
     
    tjkuster, Oct 31, 2007 IP
  5. e96

    e96 Active Member

    Messages:
    299
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    80
    #5
    sorry man, i meant to write
    alert(document.form1.giftwrapprice.value )
    
    
    the parseInt should work. However, I looked at your page and for some reason there is a problem with finding the function total().  
    
    Change the name of total() to    atotal();  and make the onClick ='atotal();' 
     and it should work-- i just copied your page's html and it worked for me. At this point it will call the function properly but i didn't test to see if it gets the total so try those alert boex in the body of the function.
    Code (markup):
     
    e96, Nov 1, 2007 IP
  6. tjkuster

    tjkuster Guest

    Messages:
    3
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Thank you so much!! Both of you. Combined, I FINALLY have a functioning order form!!
     
    tjkuster, Nov 1, 2007 IP