Calculator Using Html & Javascript


<!DOCTYPE html>
<html>

<head>

<title>CALCULATOR</title>
    <script>
        function displayvalue(n)
        {
            /*var txt = document.getElementById("result").value;
            txt = txt + n;*/
            result.value = result.value + n;
        }

        function cleartextbox()

        {
            document.getElementById("result").value = "";
        }

        function calculate()

        {
            var cal = eval(document.getElementById('result').value);
            result.value = cal;
        }

        function KeyPressed(e) {


            if (e.keyCode == 13) {

                document.getElementById('buttonequal').click();
                return false;
            }
            return true;
        }


      

</script>

</head>

   
<body>
<table align="left" border="1">
<td align="center"><h1><b>CALCULATOR</b></h1>

<p>

    <input type="text" id="result" onkeypress="return KeyPressed(event)">

</p>


<p>

    <input type="button" value="Other Function" id="buttonother" onclick="">
    <input type="button" value="C" id="buttonc" onclick="cleartextbox()">

</p>


<p>

    <input type="button" value="7" id="button7" onclick="displayvalue(button7.value)">
    <input type="button" value="8" id="button8" onclick="displayvalue(button8.value)" >
    <input type="button" value="9" id="button9" onclick="displayvalue(button9.value)">
    <input type="button" value="+" id="buttonplus" onclick="displayvalue(buttonplus.value)" >

</p>


<p>

    <input type="button" value="4" id="button4" onclick="displayvalue(button4.value)" >
    <input type="button" value="5" id="button5" onclick="displayvalue(button5.value)">
    <input type="button" value="6" id="button6" onclick="displayvalue(button6.value)">
    <input type="button" value="-" id="buttonminus" onclick="displayvalue(buttonminus.value)" >

</p>


<p>

    <input type="button" value="1" id="button1" onclick="displayvalue(button1.value)">
    <input type="button" value="2" id="button2" onclick="displayvalue(button2.value)">
    <input type="button" value="3" id="button3" onclick="displayvalue(button3.value)">
    <input type="button" value="*" id="buttonmulti" onclick="displayvalue(buttonmulti.value)">

</p>


<p>

    <input type="button" value="0" id="button0" onclick="displayvalue(button0.value)">
    <input type="button" value="." id="buttondot" onclick="displayvalue(buttondot.value)">
    <input type="button" value="=" id="buttonequal" onclick="calculate()">
    <input type="button" value="/" id="buttondivide" onclick="displayvalue(buttondivide.value)">

</p>


</td>


</table>


</body>

</html>

Comments