I’m a beginner to using Javascript in this way and this is my first attempt at building a small project.
Im needing help with adding the 2 numbers together and then displaying the answer next to the = sign. You can find the source code below :
I’m a beginner to using Javascript in this way and this is my first attempt at building a small project.
Im needing help with adding the 2 numbers together and then displaying the answer next to the = sign. You can find the source code below :
Try leaving off the “value” until you call it in the function.
As said, you need to get the value
property inside the click handler, not just once when the code first loads.
The first time your code runs .value
will be an empty string. You need to get the value the user entered after having clicked the button.
Ok thanks that’s worked and i understand how that works.
The next thing im trying is to display the result of the addition next to the = sign. How would i go about doing this?
Remove the .value
from document.getElementById('result')
selector (I assume that is where you want the output).
Then use an element property for adding content to an element, you can have a look at innerText or innerHTML.
You obviously need to do the math first as well before adding the result to the DOM, so just remember the values you get from an input
element are Strings, not Numbers so you will need to convert the string to a number before doing the addition. Use Google to look for how to do the type conversion.
Yea thanks, I just added .innerHTML to the finalResult and its working, thanks for your help guys appreciated.