I try to make a simple calculator using HTML + CSS + Javascript, couple thing I stuck at for hours, I also check around on W3 school, code pen and here.
Really appreciate you guys help for the newbie.
The problem I have here:
- Can't get the value from my button to my javascript, and I try to use eval to do the math.
- I try to let the button value show up my textarea.
Thank you guys so much!
my part of button HTML code:
<body>
        </div> -->
    <table>
          <tr>
            <th>
            <button class="key" type="button" value="clear" name="button"> C </button>
            <input class="answer2" type="text" name="" value=""
         </tr>
        <tr>
            <th>
            <button type="button" value= "7" name="button"> 7 </button>
            <button type="button" value= "8" name="button"> 8 </button>
            <button type="button" value= "9" name="button"> 9 </button>
            <button id="bnt1" type="button" value= "/" name="button"> / </button>
            <th>
         </tr>
Here's my javascript code
let button = document.querySelectorAll("button")
// let bntumber = document.querySelectorAll("button").value // this one I got get undefined.
console.log(button) // just checking my button selector is working or not.
///////// Make the magicial math happen //////////
function calulator () {
    let result = [];
    for (let i = 0 ; i < button.length ; i ++) { 
// go through the loop, select button to create value
        button.addEventListener("click", function(){
           result.push(button[i].value)    
// button value add in the result array
        })
    }
    return button
    console.log(eval(result));
}
///////////make the button value show up in the textarea///////////////
function showtext() {
    let text = document.querySelector("button")
    let showtext = document.getElementsByClassName("answer2");
    showtext.innerHTML = text.value;
}
////////after get the result, press "c" button to clear the setting////////
function cleartext () {
    let text = document.querySelector("button")
    let showtext = document.getElementsByClassName("answer2");
    showtext.innerHTML = text.value;
}
I don't put CSS here. BIG THANKS!
 
     
     
    