I have developed my version of the Windows calculator, but I am facing a challenge with the "memory tab."
My task is to create click events for the three buttons - MC, M+, M- located at the top right corner of the screen:
https://i.sstatic.net/Gd8CC.jpg
The issue is that these buttons (along with other elements) are generated every time I click "MS" on the main calculator situated on the left side of the screen, above the main buttons.
function addMemoryValue(){
const uls = document.createElement("ul");
const lis = document.createElement("li");
const h2s = document.createElement("h2");
h2s.textContent = Number(display.textContent);
const h3s = document.createElement("h3");
uls.appendChild(lis);
lis.appendChild(h2s);
h2s.appendChild(h3s);
let valueBtn = ["MC", "M+", "M-"];
valueBtn.forEach((element) => {
const memoryBtn = document.createElement("button");
memoryBtn.setAttribute("value", element);
memoryBtn.textContent = element;
const memorySpn = document.createElement("span");
memorySpn.appendChild(memoryBtn);
memoryBtn.classList.add("btnsave");
h3s.appendChild(memorySpn);
const buttonTest = document.getElementsByClassName("btnsave");
console.log(buttonTest.textContent);
})
I aim to iterate through these buttons and set up if statements based on their textContent, but I am unsure how to target the buttons since they are not yet present.
If, for example, I attempt the following:
const buttonTest = document.getElementsByClassName("btnsave");
console.log(buttonTest.textContent);
I receive "undefined," regardless of whether console.log
is inside or outside the function.