I have developed Calculator with HTML, CSS, Javascript. Here is source:
function c(val)
{
document.getElementById("chasaweriveli").value=val;
}
function v(val)
{
document.getElementById("chasaweriveli").value+=val;
}
function e()
{
try
{
c(eval(document.getElementById("chasaweriveli").value))
}
catch(e)
{
c('Error')
}
}
<!DOCTYPE html>
<html>
<head>
<title>Calculator By Gh0st</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
</head>
<body>
<ul>
<input id="chasaweriveli" type="text" size="10" readonly=""/>
<input type="button" value="C" onclick="c("")" class="btn btn-warning"/>
</ul>
<ul>
<input type="button" value="7" onclick="v("7")" class="btn btn-info"/>
<input type="button" value="8" onclick="v("8")" class="btn btn-info"/>
<input type="button" value="9" onclick="v("9")" class="btn btn-info"/>
<input type="button" value="+" onclick="v("+")" class="btn btn-success"/>
</ul>
<ul>
<input type="button" value="4" onclick="v("4")" class="btn btn-info"/>
<input type="button" value="5" onclick="v("5")" class="btn btn-info"/>
<input type="button" value="6" onclick="v("6")" class="btn btn-info"/>
<input type="button" value="-" onclick="v("-")" class="btn btn-success"/>
</ul>
<ul>
<input type="button" value="1" onclick="v("1")" class="btn btn-info"/>
<input type="button" value="2" onclick="v("2")" class="btn btn-info"/>
<input type="button" value="3" onclick="v("3")" class="btn btn-info"/>
<input type="button" value="*" onclick="v("*")" class="btn btn-success"/>
</ul>
<ul>
<input type="button" value="0" onclick="v("0")" class="btn btn-info"/>
<input type="button" value="Equals" onclick="e()" class="btn btn-danger"/>
<input type="button" value="/" onclick="v("/")" class="btn btn-success"/>
</ul>
</body>
</html>
I want to add history feature bottom of my calculator.When I'll calculate something I want to save it bottom of my calculator or somewhere else. I want to o it with Javascript. How I can do that?
History must look like this 2+2=4
First of all, you need to use proper html. a user list
<ul>
can only contain list items<li>
as direct children. if you dont like the result, simply style the list items or dont use user lists.Second, if you need to keep history just append the value of the textbox to the history element prior to evaluating the formula in it:
*Third:i would advise against using
eval()
as its behavior is unpredictable and can be compromised. better parse the formula and process the calculations via predefined functions.