I'm working on an assignment in my beginner Javascript class that involves an element document.getElementById on the first line below the var total. There is an error of missing ";" but the semicolon is there. So I know that it must be something else. Here is the code. Any suggestions?
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Price Calculator</title>
<script type="text/javascript">
function fixOrder(){
const TAX = 0.975;
var numPrice = parseFloat(document.getElementById("cost").value);
var subtotal = numPrice * TAX;
var total = subtotal + tax;
document.getElementById("cost").value = "$" cost.ToFixed(2);
document.getElementById("tax").value ="$" tax.ToFixed(2);
document.getElementById("total").value ="$" total.ToFixed(2);
}
function placeOrder(){
if (document.getElementById("cost") == ""
isNaN(document.getElementById("cost")
alert("Sorry,you must enter a numeric value to place order")
if (document.getElementById("tax") == ""
isNaN(document.getElementById("tax")
alert("Sorry, you must enter a numeric value to place the order");
}
</script>
</head>
<body bgcolor="#00f3F1">
<frame align="left">
<legend>
<h1 align="center">Price Calculator</h1>
</legend>
<form name="purchases" action="donut.php" method="POST">
Price:<p> <input type="text" id="cost" name="cost" value="" onchange="fixOrder">
</p>
Tax:<p> <input type="text" id="tax" name="tax" value="" onchange="fixOrder">
</p>
Total:<p> <input type="text" id="total" name="total" value="" >
</p>
</form>
</frame>
<div id="cost" name="cost" value="" onchange="placeOrder();"></div>
</body>
</html>
In javascript, you concatenate strings with the +
operator. Instead of
document.getElementById("cost").value = "$" cost.ToFixed(2);
Use
document.getElementById("cost").value = "$" + cost.ToFixed(2);
Your if statements are also wrong. Instead of
if (document.getElementById("cost") == ""
isNaN(document.getElementById("cost")
alert("Sorry,you must enter a numeric value to place order")
Use
if(isNaN(document.getElementById("cost").value))
alert("Sorry,you must enter a numeric value to place order");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With