-2

This is my code. When I click the calculate button it is displaying just the number 35. WHere have I gone wrong and how can I fix it?

<!doctype html>
<html>
<head>
<title>JS Practise 2</title>
</head>

<body>
<h1> Problem 1.1 </h1>
<script language="javascript">
var topsoil_amount = 1;
function calculate() {
 var topsoil_amount = document.getElementById(Input);
 var topsoil_cost = 15;
 var cost = (topsoil_amount * topsoil_cost) + 35; 
 alert(cost);
}

</script>
<br>
<input type="text" name="Input" size="16" id="Input">
<input type="button" value="Calculate" onClick="calculate()">
</body>
</html>
  • It should be `var topsoil_amount = document.getElementById(Input).value;` Check out this: http://stackoverflow.com/questions/27875951/how-to-get-users-input-text-using-javascript?lq=1 – Rikas Oct 23 '15 at 04:45
  • 1
    @Sakir - it should be - `var topsoil_amount = document.getElementById("Input").value;` – Pedram Oct 23 '15 at 04:50

1 Answers1

1

topsoil_amount is a DOM element, not a number. You need to reference the value. Secons issue is you need quotes around the id.

var topsoil_amount = document.getElementById("Input").value;
epascarello
  • 204,599
  • 20
  • 195
  • 236