Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to make Number appear in display Screen in Javscript?

I am currently building a calculator using Javascript and I am struggling to get the number to display on the screen.

Javascript code:

const num9 = document.getElementById("9").innerHTML;
const num8 = document.getElementById("8").innerHTML;
const num7 = document.getElementById("7").innerHTML;
const num6 = document.getElementById("6").innerHTML;
const num5 = document.getElementById("5").innerHTML;
const num4 = document.getElementById("4").innerHTML;
const num3 = document.getElementById("3").innerHTML;
const num2 = document.getElementById("2").innerHTML;
const num1 = document.getElementById("1").innerHTML;
const num0 = document.getElementById("0").innerHTML;
const btnPlus = document.getElementById("plus").innerHTML;
const btnMinus = document.getElementById("minus").innerHTML;
const btnDivide = document.getElementById("divide").innerHTML;
const btnMultiply = document.getElementById("multiply").innerHTML;
const btnEquals = document.getElementById("equals").innerHTML;
let display;
let result;


function displayScreen()
{
  if(num9 == num9)
  {
      display = document.getElementsByClassName("calcDisplay").innerHTML = num9;
      console.log(display);
  }
}

The output so far is if button 9 is pressed it shows up in the console, but does not replace the zero to 9 in the display screen. isn’t the .innerHTML = num9 meant to replace the 0 number to 9?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

The "calcDisplay" is a class on the HTML side.

>Solution :

getElementsByClassName returns an array, use [0] to access the first element

document.getElementsByClassName("calcDisplay")[0].innerHTML = num9
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading