Question

Just wondering if anyone can advise on how to get the evaluated answer at the end of this piece of JS to read on the display ID that I have created?

function setup() {
var i;
for (i = 0; i <= 9; i++) {
document.getElementById(i).onclick = handleInput;
document.getElementById("operator_*").onclick = handleInput;
document.getElementById("operator_/").onclick = handleInput;
document.getElementById("operator_.").onclick = handleInput;
document.getElementById("operator_-").onclick = handleInput;
document.getElementById("operator_+").onclick = handleInput;

var x;  
document.getElementById("operator_=").onclick = evaluateInput;

}

}

function handleInput(e) {
var s = document.getElementById("display").childNodes[0];
s.nodeValue += e.srcElement.childNodes[0].nodeValue ;
console.log(e.srcElement.id);

}

function evaluateInput(e) {
console.log(eval(document.getElementById("display").childNodes[0].nodeValue));

}

Many thanks...

Was it helpful?

Solution

Do you have any advice for the issue at hand?

As a matter of fact, I do :)

DEMO

function setup() {
    var i;
    for (i = 0; i <= 9; i++) {
        document.getElementById(i).onclick = handleInput;
        document.getElementById("operator_*").onclick = handleInput;
        document.getElementById("operator_/").onclick = handleInput;
        document.getElementById("operator_.").onclick = handleInput;
        document.getElementById("operator_-").onclick = handleInput;
        document.getElementById("operator_+").onclick = handleInput;

        //var x;
        document.getElementById("operator_=").onclick = evaluateInput;
    }
}

function handleInput(e) {
    var s = document.getElementById("display");
    s.innerHTML += e.srcElement.childNodes[0].nodeValue;
}

function evaluateInput(e) {
    var s = document.getElementById("display");
    s.innerHTML = eval(document.getElementById("display").childNodes[0].nodeValue);
}

setup();
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top