+ 1

Something's wrong!

I made this calculator but when the result is displayed it erases everything and displays only the result. How can I correct this fault? https://code.sololearn.com/W8i4VdYipwD5/?ref=app

26th Sep 2017, 8:14 AM
B.D
B.D - avatar
10 Answers
+ 2
Here's the fix to your code but study it. Determine what's different in this code and yours. https://code.sololearn.com/W2p8Ld5CmfWc/?ref=app
26th Sep 2017, 9:27 AM
Ghauth Christians
Ghauth Christians - avatar
+ 5
I don't believe you made this code, but I'll help you anyway. The screen goes blank and then display the result because that's what the code says it should do after calculating the result. So you should tell the code to put the result elsewhere, like in the header tag for example. Example (HTML): //1. Give the header tag an ID called "result" <h1 id="result">My Calculator<h1/> //2. Direct the output of the calculation to that tag document.getElementById("result").innerHTML=1+1; Note: Don't use document.write()
26th Sep 2017, 8:30 AM
Ghauth Christians
Ghauth Christians - avatar
+ 3
I advice you to do the Javascript course if you haven't already. The course will explain better and in detail what these functions do. In short, document.getElementById does exactly what it says, it selects one of your elements by its unique ID. After selecting this element, you can now manipulate it, that's where innerHTML comes in. innerHTML manipulates the HTML code within that tag. So to summarise, document.getElementById("result").innerHTML -selects the tag with the ID "result" and gets it ready to manipulate the HTML code within
26th Sep 2017, 9:38 AM
Ghauth Christians
Ghauth Christians - avatar
+ 2
Because you made it possible only for the division code to be valid. You made four window.onload functions instead of one. So you basically telling your code to load addition then restart and load subtraction instead then restart and load multiplication instead then restart and load division instead. In the end, only division's code is valid for use. You must enclose your entire code in one window.onload and you must make the variables unique. You shouldn't have 4 "btn" variables, make it btnAdd, btnSub, btnMult, btnDiv. This counts for ALL variables.
26th Sep 2017, 8:46 AM
Ghauth Christians
Ghauth Christians - avatar
+ 2
Thanks
26th Sep 2017, 8:48 AM
B.D
B.D - avatar
+ 1
And why aren't the other buttons working?
26th Sep 2017, 8:38 AM
B.D
B.D - avatar
+ 1
The code is not working properly!
26th Sep 2017, 8:40 AM
B.D
B.D - avatar
+ 1
It displays error when I apply the ".document.getelementbyid" function.
26th Sep 2017, 9:20 AM
B.D
B.D - avatar
+ 1
What is the function of innerHTML and document.getElementById?
26th Sep 2017, 9:30 AM
B.D
B.D - avatar
0
Apply the changes and if you still get stuck then feel free to ask for help again.
26th Sep 2017, 8:55 AM
Ghauth Christians
Ghauth Christians - avatar