简体   繁体   中英

Javascript calculator not working.

I created this javascript calculator a little while ago. It worked perfectly for me for ages, but after going back to it I have discovered that it simple doesn't work at all anymore.

Here is my js fiddle... http://jsfiddle.net/AdamMartin121/XXFXv/

This is the code I used. It's very long and probably unnecessary. Does anyone know firstly why it's not working, and secondly a more efficient way of doing this?

Javascript Code...

    var display = document.getElementById("display"); 

document.addEventListener("DOMContentLoaded", init, false);


function init() {

var name = prompt("Enter your name", "");
    if (name == null)
    {
document.getElementById("head").innerHTML="Enter a name!";      
}    
else 
{
    document.getElementById("head").innerHTML= "<p>Welcome, " +name+ ". </p> <p2> This is a simple Javascript calculator. It is open source and was designed to help people to learn Javascript. <br> Feel free to copy and paste the code, and use it how you wish. </p>";
 }
 document.getElementById("calc").display.style.block;
 }
function naught() {
var display= document.getElementById("display");    
    display.value+="0";
    }
function one() {
    var display= document.getElementById("display");
    display.value+="1"
    }
function two() {    
    var display= document.getElementById("display");
    display.value+= "2";
    }
function three() {
    var display= document.getElementById("display");
    display.value+="3";
    }
function four() {
    var display= document.getElementById("display");
    display.value+="4";
    }
function five() {
    var display= document.getElementById("display");
    display.value+="5";
    }
function six() {
    var display= document.getElementById("display");
    display.value+="6";
    }
function seven() {
    var display= document.getElementById("display");
    display.value+="7";
    }
function eight() {
    var display= document.getElementById("display");
    display.value+="8";
    }
function nine() {
    var display= document.getElementById("display");
    display.value+="9";
}
function add() {
    var display= document.getElementById("display");
    display.value+="+";
}
function take() {
    var display= document.getElementById("display");
    display.value+="-";
}
function times() {
    var display= document.getElementById("display");
    display.value+="*";
}
function divi() {
    var display= document.getElementById("display");
    display.value+="/";
}
function equal() {
    var display= document.getElementById("display");
        display.value= eval(display.value);
}

Thanks in advance.

This line:

document.getElementById("calc").display.style.block;

Should be this (style comes before display):

document.getElementById("calc").style.display.block;

Also, you're selecting it in JavaScript by the ID calc , but in your HTML table, you've given it the class calc ( <table class="calc"> ) instead, so change that to ID ( <table id="calc"> ) and voila.


Working jsFiddle here.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM