+ 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
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
+ 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()
+ 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
+ 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.
+ 2
Thanks
+ 1
And why aren't the other buttons working?
+ 1
The code is not working properly!
+ 1
It displays error when I apply the ".document.getelementbyid" function.
+ 1
What is the function of innerHTML and document.getElementById?
0
Apply the changes and if you still get stuck then feel free to ask for help again.