Skip to main content

Using Functions: Activity 1 (Part 2)

To use the parseInt() function, change the file as shown below. Save the file. Return to the browser and refresh it. Enter a natural number in the top text box and click Calculate.

 
<html>
<head>
<SCRIPT language="JavaScript">
function calculateOrder()
{
    var quantity, totalOrder;
    quantity = parseInt(document.frmOrder.txtQuantity.value);
    totalOrder = quantity * 2.95;
    document.frmOrder.txtOrderTotal.value = totalOrder;
}
</SCRIPT>
<title>CD Publisher</title>
</head>
<body>
<h1>CD Publisher</h1>
<form name="frmOrder">
  <table border="0" width="344">
    <tr>
      <td width="231">Number of CDs ordered:</td>
      <td width="93"><input type="text" name="txtQuantity" size="10" value="0"></td>
      <td width="1"><input type="button" value="Calculate" name="btnCalculate" onClick="calculateOrder()"></td>
    </tr>
    <tr>
      <td width="211" colspan="3">At $2.95 for each CD</td>
    </tr>
    <tr>
      <td>Order Total:</td>
      <td width="70"><input type="text" name="txtOrderTotal" size="10"></td>
      <td width="70"><input type="reset" value="Reset" name="btnReset"></td>
    </tr>
  </table>
</form>
</body>
</html>
 

Next: Event Handlers