-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcalculator.html
More file actions
57 lines (51 loc) · 1.96 KB
/
calculator.html
File metadata and controls
57 lines (51 loc) · 1.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Simple Calculator implemented in javaScript</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.0/jquery.js"></script>
<script>
$(function () {
// Add containers to the DOM
var $calculator = $('<div/>', {id: 'calculator'}).appendTo('body');
var $input = $('<input/>', {id: 'input'}).appendTo($calculator);
var $buttons = $('<div/>', {id: 'buttons'}).appendTo($calculator);
// Add buttons to the DOM
$.each('1234567890.=+-*/←C'.split(''), function () {
var $button = $('<button/>', {text: this.toString(), click: function () {
// Handle button clicks
switch ($(this).text()) {
// '=' will fetch the current expression string, evaluate it,
// and write the result back into the input/output field.
// That's where the actual calculation happens.
// eval($input.val()) will calculate the value and after that,
// it will pass as an argument to $input.val means pringting it in input field.
case '=': try {$input.val(eval($input.val()));}
catch (e) {$input.val('ERROR');}
break;
//'C' will clear the input/output field
case 'C': return $input.val('');
break;
// 'CE' will delete the last character from the input/output field
case '←': return $input.val($input.val().replace(/.$/, ''));
break;
// All other buttons will add a character to the input/output field
default: $input.val($input.val() + $(this).text());
}
}}).appendTo($buttons);
});
});
</script>
<script>
var loc = window.location.href;
//alert(loc);
</script>
<style type="text/css">
#calculator {border: 1px solid; width: 130px;}
#input {border: 1px solid; height: 20px; width: 114px; margin: 5px; padding: 2px; text-align: right;}
#buttons {margin: 5px;}
button {width: 30px; height: 30px;}
</style>
</head>
<body class="calculator calc"></body>
</html>