Forum Moderators: open
Here is what I have but it's giving me errors.
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<title>Page title</title>
<script type="text/javascript" language="JavaScript">
function check(delivery_type)
{
document.getElementById("answer").value=delivery_type
}
function calculate ()
{
var idx = document.ticket.location.selectedIndex;
var location = document.ticket.location[idx].value
var quantity = document.ticket.quantity.value;
var delivery_type = document.ticket.delivery_type.value;
var subtotal = document.ticket.subtotal.value;
var subtotal = (location*quantity);
document.ticket.subtotal.value = subtotal;
document.ticket.grandtotal.value = subtotal + delivery_type;
}
</script>
</head>
<body>
<form id="ticket" name="myform">
<h3>Ticket Cost Calculator </h3><br />
<br />
<table border="0">
<tr>
<td>Location:</td>
<td>Quantity:</td>
</tr>
<tr>
<td><select name="location" onChange="calculate()">
<option value="10">End zone $10</option>
<option value="20">Not bad $20</option>
<option value="30">Good $30</option>
</select></td>
<td><input type="text" name="quantity" ></td>
</tr>
<tr>
<td>Subtotal:</td>
<td><input type="text" name="subtotal" readonly="readonly"></td>
</tr>
<tr>
<td><input type="radio" name="delivery_type" onchange="check(this.value)" value="15">Will call ($5) or</td>
<td><input type="text" name="answer" readonly="readonly"></td>
</tr>
<tr>
<td><input type="radio" name="delivery_type" onchange="check(this.value)" value="5">Overnight Delivery ($15)</td>
<td> </td>
</tr>
<tr>
<td>GrandTotal:</td>
<td><input type="text" name="grandtotal" readonly="readonly"></td>
</tr>
<tr>
<td><input type="button" name="calculate" value="Calculate" onClick="calculate(document.getElementById('ticket').grandtotal.value);"> </td>
<td> <input type="reset" value="Clear"></td>
</tr>
</table>
</form>
</body>
</html>
Also, as I already mentioned in one of your other threads, there is a fully functional example in your thread with the subject "Question!" [webmasterworld.com]
In any case, look at the computeGrandTotal method in my other thread. That does (I think) exactly what you are trying to do. All you need to do is tie that method to the event of your choice. In this case, it sounds like you want it to happen on a button 'onclick' event.
As I posted in the other thread, you're better to attach your event handlers via JavaScript instead of using inline event handlers. Also, if you're using XHTML, then all of your attributes must be lowercase (meaning if you're going to put them inline, then it needs to be 'onclick=...' instead of 'onClick=...'). If you validate your page, it will tell you where these errors are.