Click here to Skip to main content
16,015,756 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I made a web calculator in which I used external js file and css. css is working nice but js is not.

The html code -

<html>
<head>
<title>webcallculator</title>
<link rel="stylesheet" href="style.css">
</head>
<body id="body">
<h1 id="h1">INPUT YOUR NUMBERS</h1>



<input type="text" id="box1"  >+
<input type="text" id="box2" >
<input type="button" value="=" onclick='calsum()'>
<input type="text" id="result" readonly>
<hr>


<input type="text" id="box3" >-
<input type="text" id="box4" >
<input type="button" value="=" onclick='calsumsub()'>
<input type="text" id="resultsub" readonly>

<script type="text/javascript" src="script1.js"></script> 
</body> 


</html>


the css code -

#Input{ bodor: none;
       color: #3344ee
}
#h1 { font-face: comic-sans-ms;
}
#body {background-color: peachpuff;
}


the js code -
function calsum(){
let box1 = document.getElementById("box1").value;
let box2 = document.getElementById("box2").value;
let sum = Number(box1) + Number(box2);
document.getElementById("result").value = sum;
} 

function calsumsub(){
let box3 = document.getElementById("box3").value;
let box4 = document.getElementById("box4").value;
let sumsub = Number(box3)-Number(box4);
document.getElementById("resultsub").value = sumsub;
} 
window.alert("hi how are you" + w)
var w,a;
let w = 3;

js file name - script1.js

I am using safari 14 browser and sublime editor.

What I have tried:

I have checked the safari console and checked the internet a lot but it shows error for js file.
Posted
Updated 25-May-21 11:56am
Comments
SeanChupas 25-May-21 14:32pm    
What is the error?

1 solution

Do you ever see the alert() from the JS file ?
Does the following line run when you load your the HTML file in your browser?
JavaScript
window.alert("hi how are you" + w)


I'm guessing that it does not.

If it doesn't then the HTML file may not be located in the same folder where the JS file is located and so it cannot load the JS file because it cannot find it.

Are the two files in the same folder?
How about a JSBIN? Here's your code -- I fixed it and it runs:
JS Bin - Collaborative JavaScript Debugging[^]
Main Problem
The main problem was that you were redefining the w variable and it was making all your js fail.

You had:
JavaScript
var w,a;
let w = 3;

That defines w with the old var keyword.
Then you attempted to redefine w again with teh new let keyword.

of course, a is not used anywhere anyways.
So just make it
JavaScript
let w = 3;


Take a look at the jsbin I created. it works.
Also, don't use getElementById()
Switch to using document.querySelector("#idname");
You see it in the jsbin.
Good luck.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900