<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<label id="aLabel">Side A:</label><br>
<input type="text" id="aTextBox"><br>
<label id="bLabel">Side B:</label><br>
<input type="text" id="bTextBox"><br>
<button type="button" id="submit">submit</button><br>
<label id="cLabel"></label><br>
<script src="index.js"></script>
</body>
</html>
let a;
let b;
// Calculate the length of the hypotenuse using the Pythagorean theorem
document.getElementById("submit").onClick = function(){
a = document.getElementById("aTextBox").value;
a = Number(a)
b = document.getElementById("bTextBox").value;
b = Number(b);
let c = Math.sqrt(Math.pow(a, 2) + Math.pow(b, 2));
document.getElementById("cLabel").innerHTML = c;
console.log("The hypotenuse of the triangle is: " + c);
}
I believe I acquired all the ids in the DOM correctly and also imported the js file correctly(it is named index.js and in the same folder, in fact it auto populates when typing)
I’m stumped, it should be a simple equation, but I don’t know what I’m doing wrong or what alternatives to use. I really appreciate any coding help the community can give.
3
Answers
It is not working because the of the onclick you added after you get the submit button. I rewrote the code for you. Please let me know if this works and is what you are looking for.
Change
onClick
toonclick
your error here is that your using onClick writted in camel case, you can fix your error by changing onClick to onclick, simply replace your javascript code by this following code:
Note: in this JavaScript code, its a bad practice to declare a variables outside the place were you are using them instead you can refactor your code to be more optimized :
Your code will then be like that:
This way your code became more readable. try always to make your code cleaner as much as possible so that debugging errors will be an easy task for you. Good luck!