sammyb123 sammyb123 - 6 days ago 6
HTML Question

Getting the value from an HTML button using JavaScript

I am making a calculator, I have already created the calculator in html and css but I am trying to move forward by making the button clicks register in the display which is what my problem is right now. I am fairly new to JavaScript so if someone could point me in the right direction on how to do it or where to find the answer I would appreciate it.

This is a the portion I am working on, trying to get button '7' to register so I can do the others.

<div class="container-fluid calc" >
<div class="display">
<label type="text" id="screen">0</label>

<div class="buttons">

<button onClick='calculate()' id='myButton'>7</button>
<button>8</button>
<button>9</button>


Here is the JS I put together

function calculate(){
var num = document.getElementById('#myButton').contentValue;
document.getElementById('screen').innerHTML = num;
}

calculate();

Answer

You need to update from

 var num = document.getElementById('#myButton').contentValue;

to

 var num = document.getElementById('myButton').innerHTML;
Comments