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.

<SCRIPT language="JavaScript">
function calculateOrder()
    var quantity, totalOrder;
    quantity = parseInt(document.frmOrder.txtQuantity.value);
    totalOrder = quantity * 2.95;
    document.frmOrder.txtOrderTotal.value = totalOrder;
<title>CD Publisher</title>
<h1>CD Publisher</h1>
<form name="frmOrder">
  <table border="0" width="344">
      <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>
      <td width="211" colspan="3">At $2.95 for each CD</td>
      <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>

Next: Event Handlers