1

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...

owlwink
  • 91
  • 9
  • 1
    ID and NAME tokens must begin with a letter ([A-Za-z]) and may be followed by any number of letters, digits ([0-9]), hyphens ("-"), underscores ("_"), colons (":"), and periods (".") – kei Nov 19 '13 at 19:59
  • @kei: I have updated the syntax. Do you have any advice for the issue at hand? Cheers! – owlwink Nov 19 '13 at 20:09
  • @owlwink You should not use eval if you can avoid it and this is something you can do without eval. With that said, is the console showing the correct answer and you just want to replace the contents of the "display" element with the result from the eval or does the console not show the correct value? If the console shows the right value you can just assign the value to a variable, eval returns the value. If it's not showing then you should make a JSFiddle because we can't see much like this. – Bikonja Nov 19 '13 at 20:18
  • @Bikonja: The console is returning the correct answer. Is it just a case of setting the eval to a variable or does there need to be an additional one? Thanks :-) – owlwink Nov 19 '13 at 20:22
  • @owlwink Just use the expression `eval(document.getElementById("display").childNodes[0].nodeValue)` as you would any other expression, for instance `var result = eval(document.getElementById("display").childNodes[0].nodeValue)`. – Bikonja Nov 19 '13 at 20:24
  • @Bikonja: I have tried that and it is still working correctly in the console, but not appearing in my display. Any other ideas? Many thanks... – owlwink Nov 19 '13 at 20:31

1 Answers1

1

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();
kei
  • 20,157
  • 2
  • 35
  • 62
  • Thanks, perfect resolution to my problem. – owlwink Nov 19 '13 at 20:35
  • @owlwink Glad to see your problem resolved, but as far as I can tell, this is exactly what I suggested in the comment so I'm curious, why didn't it work? Can you post the exact line(s) you tried? Would be nice to know if there's a caveat where this does not work. – Bikonja Nov 20 '13 at 08:39
  • @Bikonja: I think it was just cache related. Thanks again for your help :-) – owlwink Nov 23 '13 at 21:47