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>
As has already been pointed out by the numerous other answers, there are a multitude of problems with your code. This answer is intended to address all of the mistakes and shortcomings.
I’ll go from the top of the posted code. Firstly, you may have simply not pasted it into the question, but you’re missing a
DOCTYPEdeclaration at the top of the file. All HTML documents should begin with aDOCTYPE, for example:<!DOCTYPE html>Into the JavaScript, the
constkeyword, while valid, is not supported by a lot of older browsers and is definitely best avoided. Replace it withvar, it will make no difference.To concatenate strings in JavaScript you use the
+operator:document.getElementById("cost").value = "$" + cost.toFixed(2);Also notice that I’ve changed
ToFixedtotoFixed. Another problem with the above line is the variablecost, which you’ve not defined. You may have meantnumPrice, but I’m not entirely sure!On the next line, you’re using
taxinstead ofTAX. Variable names in JavaScript are case-sensitive, sotaxis also undefined.Your
ifstatement conditions are wrong. I’m assuming you meant this:There’s a few changes to that line. Firstly, you were comparing a DOM element itself (
getElementByIdreturns a DOM element), rather than the value of it. Secondly, you were missing the closing parentheses, and finally, you needed some operator between the two conditions. I think you were after the “or” operator,||. There’s a similar issue with your secondifstatement.You’re missing a semi-colon off the end of your first
alertline. While that’s still valid, it’s definitely good practice to always include the semi-colon.Back into the HTML, you’re trying to call the
fixOrderJS function, but you’ve missed the parentheses:To be really picky, you shouldn’t really use inline event handlers, but I won’t go into that here.
I’m not sure why you’re using a
frameelement the way you are, but I have a feeling it would be better replaced by adiv, or simply nothing at all (again, I may not have the full picture if you haven’t posted the full code).On your
bodytag you’re using thebgcolorattribute. That should definitely be replaced by CSS. You can use thebackground-colorproperty.