This code demonstrates to create a JQuery Calulator.
<html
xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<script type="text/javascript" src="Scripts/jquery-1.4.1.min.js"></script>
<link rel="stylesheet" media="screen, print, handheld" type="text/css" href="calc.css" />
<title>JqCalculator</title>
</head>
<body>
<div style="width:200px;" class="main">
<center>JqCalculator</center>
<form name="calce" action="" id="calculator">
<input type="hidden" id="operation" value=""/>
<input type="text" id="text"
style="width:98%; height:35px; text-align:right; background-color: #000000; color: #FFFFFF;" readonly="readonly"/>
<input type="text" id="answer" name="tex" value="0" style="width:98%; height:35px; text-align:right;" readonly="readonly"/>
<input type="button" value="Back Sapce" id="bac" class="op" style="width:100px; height:50px; float:left; color:red;"/>
<input type="button" value="C" id="C" class="op" style="width:50px; height:50px; float:left; color:red;"/>
<input type="button" value="/" id="divide" class="op" style="width:50px; height:50px; float:left;"/>
<input type="button" value="7" id="7" class="no" style="width:50px; height:50px; float:left;"/>
<input type="button" value="8" id="8" class="no" style="width:50px; height:50px; float:left;"/>
<input type="button" value="9" id="9" class="no" style="width:50px; height:50px; float:left;"/>
<input type="button" value="*" id="product" class="op" style="width:50px; height:50px; float:left;"/>
<input type="button" value="4" id="4" class="no" style="width:50px; height:50px; float:left;"/>
<input type="button" value="5" id="5" class="no" style="width:50px; height:50px; float:left;"/>
<input type="button" value="6" id="6" class="no" style="width:50px; height:50px; float:left;"/>
<br />
<input type="button" value="-" id="subtract" class="op" style="width:50px; height:50px; float:left;"/>
<input type="button" value="1" id="1" class="no" style="width:50px; height:50px; float:left;"/>
<input type="button" value="2" id="2" class="no" style="width:50px; height:50px; float:left;"/>
<input type="button" value="3" id="3" class="no" style="width:50px; height:50px; float:left;"/>
<input type="button" value="+" id="plus" class="op" style="width:50px; height:50px; float:left;"/>
<input type="button" value="0" id="0" class="no" style="width:50px; height:50px; float:left;"/>
<input type="button" value="." id="." class="no" style="width:50px; height:50px; float:left;"/>
<input type="button" value="=" id="equals" class="op" style="width:100px; height:50px; float:left;"/>
</form>
<div style="clear:both;"></div>
</div>
</body>
</html>
< script type = "text/javascript" > $(document).ready(function(e) {
$('.no').click(function() {
var v = 0;
v = $(this).val();
if ($("#answer").val() == 0) {
$("#answer").val('');
}
$('#answer').val($('#answer').val() + v);
});
$('#C').click(function() {
$('#answer').val(0);
$('#operation').val('');
$('#operation').removeClass('activeAnswer');
$('#equals').attr('onclick', '');
$("#text").val('');
$("#bac").removeAttr('disabled');
});
$('#plus').click(function(e) {
var out = $("#answer").val();
var plu = $("#plus").val();
$("#text").val($("#text").val() + out + plu);
if ($('#answer').val() == '') {
return false;
$('#equals').attr('onclick', '');
} else if ($('#operation').attr('class') == 'activeAnswer') {
$('#operation').val($('#operation').val() + $('#plus').val());
// $('#answer').val('');
//$('#equals').attr('onclick', '');
} else {
$('#operation').val($('#operation').val() + $('#answer').val() + $('#plus').val());
$('#answer').val('');
$('#equals').attr('onclick', '');
$('#bac').attr('disabled', 'disabled');
}
});
$('#subtract').click(function(e) {
var out = $("#answer").val();
var sub = $("#subtract").val();
$("#text").val($("#text").val() + out + sub);
if ($('#answer').val() == '') {
return false;
$('#equals').attr('onclick', '');
} else if ($('#operation').attr('class') == 'activeAnswer') {
$('#operation').val($('#operation').val() + $('#subtract').val());
$('#answer').val('');
$('#equals').attr('onclick', '');
} else {
$('#operation').val($('#operation').val() + $('#answer').val() + $('#subtract').val());
$('#answer').val('');
$('#equals').attr('onclick', '');
$('#bac').attr('disabled', 'disabled');
}
});
$('#divide').click(function(e) {
var out = $("#answer").val();
var div = $("#divide").val();
$("#text").val($("#text").val() + out + div);
if ($('#answer').val() == '') {
return false;
$('#equals').attr('onclick', '');
} else if ($('#operation').attr('class') == 'activeAnswer') {
$('#operation').val($('#operation').val() + $('#divide').val());
$('#answer').val('');
$('#equals').attr('onclick', '');
} else {
$('#operation').val($('#operation').val() + $('#answer').val() + $('#divide').val());
$('#answer').val('');
$('#equals').attr('onclick', '');
$('#bac').attr('disabled', 'disabled');
}
});
$('#product').click(function(e) {
var out = $("#answer").val();
var pro = $("#product").val();
$("#text").val($("#text").val() + out + pro);
if ($('#answer').val() == '') {
return false;
$('#equals').attr('onclick', '');
} else if ($('#operation').attr('class') == 'activeAnswer') {
$('#operation').val($('#operation').val() + $('#product').val());
$('#answer').val('');
$('#equals').attr('onclick', '');
} else {
$('#operation').val($('#operation').val() + $('#answer').val() + $('#product').val());
$('#answer').val('');
$('#equals').attr('onclick', '');
$('#bac').attr('disabled', 'disabled');
}
});
$('#equals').click(function() {
$("#text").val('');
if ($('#equals').attr('onclick') != 'return false') {
var a = $('#answer').val();
var b = $('#operation').val();
var c = b.concat(a);
$('#answer').val(eval(c));
$('#operation').val(eval(c));
$('#operation').addClass('activeAnswer');
$('#equals').attr('onclick', 'return false');
$("#text").val('');
}
});
calce.tex.value != ""
$("#bac").click(function() {
$("#text").val('');
if ($("#answer").val() != "") {
document.calce.tex.value = document.calce.tex.value.substring(0, document.calce.tex.value.length * 1 - 1);
//$("#answer").val($("#answer").val()String(0,$("#answer").val().length * -1));
} else {
$("#answer").val(0);
}
});
$(".no,.op").mouseover(function() {
$(this).css({
"background-color": "#FF99CC"
});
});
$(".no,.op").mouseout(function() {
$(this).css({
"background-color": "#F0F0EB"
});
});
$("#answer").keydown(function(evt) {
if ((evt.keyCode > 95) && (evt.keyCode < 106)) {
var nbr = evt.keyCode - 96;
if ($("#answer").val() == 0) {
$("#answer").val('');
}
$('#answer').val($('#answer').val() + nbr);
if ($(".no").val() == nbr) {
$(this).css({
"background-color": "#F0F0EB"
});
}
} else if ((evt.keyCode > 47) && (evt.keyCode < 58)) {
var nbr = evt.keyCode - 48;
$('#answer').val($('#answer').val() + nbr);
} else if (evt.keyCode == 107) {
$('evt.keyCode == 8').attr('disabled', 'disabled');
var out = $("#answer").val();
var plu = $("#plus").val();
$("#text").val($("#text").val() + out + plu);
if ($('#answer').val() == '') {
return false;
} else if ($('#operation').attr('class') == 'activeAnswer') {
$('#operation').val($('#operation').val() + $('#plus').val());
$('#answer').val('');
} else {
$('#operation').val($('#operation').val() + $('#answer').val() + $('#plus').val());
$('#answer').val('');
}
} else if (evt.keyCode == 13) {
var a = $('#answer').val();
var b = $('#operation').val();
var c = b.concat(a);
$('#answer').val(eval(c));
$('#operation').val(eval(c));
$('#operation').addClass('activeAnswer');
$("#text").val('');
} else if (evt.keyCode == 109) {
var out = $("#answer").val();
var sub = $("#subtract").val();
$("#text").val($("#text").val() + out + sub);
if ($('#answer').val() == '') {
return false;
} else if ($('#operation').attr('class') == 'activeAnswer') {
$('#operation').val($('#operation').val() + $('#subtract').val());
$('#answer').val('');
} else {
$('#operation').val($('#operation').val() + $('#answer').val() + $('#subtract').val());
$('#answer').val('');
$('#bac').attr('disabled', 'disabled');
}
} else if (evt.keyCode == 106) {
var out = $("#answer").val();
var pro = $("#product").val();
$("#text").val($("#text").val() + out + pro);
if ($('#answer').val() == '') {
return false;
} else if ($('#operation').attr('class') == 'activeAnswer') {
$('#operation').val($('#operation').val() + $('#product').val());
$('#answer').val('');
} else {
$('#operation').val($('#operation').val() + $('#answer').val() + $('#product').val());
$('#answer').val('');
}
} else if (evt.keyCode == 46 || evt.keyCode == 27) {
$('#answer').val(0);
$('#operation').val('');
$('#operation').removeClass('activeAnswer');
$("#bac").removeAttr('disabled');
} else if (evt.keyCode == 8) {
if ($("#answer").val() != "") {
document.calce.tex.value = document.calce.tex.value.substring(0, document.calce.tex.value.length * 1 - 1);
} else {
$("#answer").val(0);
}
} else if (evt.keyCode == 111) {
var out = $("#answer").val();
var div = $("#divide").val();
$("#text").val($("#text").val() + out + div);
if ($('#answer').val() == '') {
return false;
$('#equals').attr('onclick', '');
} else if ($('#operation').attr('class') == 'activeAnswer') {
$('#operation').val($('#operation').val() + $('#divide').val());
$('#answer').val('');
$('#equals').attr('onclick', '');
} else {
$('#operation').val($('#operation').val() + $('#answer').val() + $('#divide').val());
$('#answer').val('');
$('#equals').attr('onclick', '');
}
}
return false;
});
}); < /script>