Created
November 29, 2018 18:26
-
-
Save Liight/e1852114e1033c11498db20b174bc50a to your computer and use it in GitHub Desktop.
Calculator (JavaScript/jQuery)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <div class="container"> | |
| <div class="row col-xs-12"> | |
| <div class="col-xs-4"></div> | |
| <div class="col-xs-4" id="calculator"> | |
| <div class="row text-center" id="title"> | |
| <h2>Calculator</h2></div> | |
| <input type="hidden" id="operation" value="" /> | |
| <input class="row" id="answer" readonly="readonly" /> | |
| <input class="row" id="display" readonly="readonly" /> | |
| <div class="button-container"> | |
| <div class="row"> | |
| <button class="well" value="AC" id="AC">AC</button> | |
| <button class="well empty" value="" id="CE"></button> | |
| <button class="well empty" value="%" id="percent"></button> | |
| <button class="well" value="/" id="divide">/</button> | |
| </div> | |
| <div class="row"> | |
| <button class="well" value="7" id="7">7</button> | |
| <button class="well" value="8" id="8">8</button> | |
| <button class="well" value="9" id="9">9</button> | |
| <button class="well" value="*" id="product">*</button> | |
| </div> | |
| <div class="row"> | |
| <button class="well" value="4" id="4">4</button> | |
| <button class="well" value="5" id="5">5</button> | |
| <button class="well" value="6" id="6">6</button> | |
| <button class="well" value="-" id="subtract">-</button> | |
| </div> | |
| <div class="row"> | |
| <button class="well" value="1" id="1">1</button> | |
| <button class="well" value="2" id="2">2</button> | |
| <button class="well" value="3" id="3">3</button> | |
| <button class="button tall-button well" value="+" id="plus">+</button> | |
| <div class="row bottom-row"> | |
| <button class="well" value="0" id="0">0</button> | |
| <button class="well" value="." id="decimalPoint">.</button> | |
| <button class="well" value="=" id="equals">=</button> | |
| </div> | |
| </div> | |
| </div> | |
| </div> | |
| <div class="col-xs-4"></div> | |
| </div> | |
| </div> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // On number click - add number clicked to the display | |
| $('#1,#2,#3,#4,#5,#6,#7,#8,#9,#0,#decimalPoint').click(function() { | |
| var v = $(this).val(); | |
| $('#answer').val($('#answer').val() + v); | |
| if ($('#display').val === '') { | |
| $('#display').val($('#answer').val()); | |
| } else { | |
| $('#display').val($('#display').val() + v); | |
| } | |
| }); | |
| // All Clear Button | |
| $('#AC').click(function() { | |
| $('#answer').val(''); | |
| $('#display').val(''); | |
| $('#operation').val(''); | |
| $('#operation').removeClass('activeAnswer'); | |
| $('#equals').attr('onclick', ''); | |
| }); | |
| // Plus Button | |
| $('#plus').click(function(e) { | |
| if ($('#answer').val() === '') { | |
| $('#equals').attr('onclick', ''); | |
| return false; | |
| } else if ($('#operation').attr('class') == 'activeAnswer') { | |
| $('#operation').val($('#operation').val() + $('#plus').val()); | |
| $('#answer').val(''); | |
| $('#display').val($('#display').val() + $(this).val()); | |
| $('#equals').attr('onclick', ''); | |
| } else { | |
| $('#operation').val($('#operation').val() + $('#answer').val() + $('#plus').val()); | |
| $('#answer').val(''); | |
| $('#display').val($('#display').val() + $(this).val()); | |
| $('#equals').attr('onclick', ''); | |
| } | |
| }); | |
| // Subtract Button | |
| $('#subtract').click(function(e) { | |
| if ($('#answer').val() === '') { | |
| $('#equals').attr('onclick', ''); | |
| return false; | |
| } else if ($('#operation').attr('class') == 'activeAnswer') { | |
| $('#operation').val($('#operation').val() + $('#subtract').val()); | |
| $('#answer').val(''); | |
| $('#display').val($('#display').val() + $(this).val()); | |
| $('#equals').attr('onclick', ''); | |
| } else { | |
| $('#operation').val($('#operation').val() + $('#answer').val() + $('#subtract').val()); | |
| $('#answer').val(''); | |
| $('#display').val($('#display').val() + $(this).val()); | |
| $('#equals').attr('onclick', ''); | |
| } | |
| }); | |
| // Divide Button | |
| $('#divide').click(function(e) { | |
| if ($('#answer').val() === '') { | |
| $('#equals').attr('onclick', ''); | |
| return false; | |
| } else if ($('#operation').attr('class') == 'activeAnswer') { | |
| $('#operation').val($('#operation').val() + $('#divide').val()); | |
| $('#answer').val(''); | |
| $('#display').val($('#display').val() + $(this).val()); | |
| $('#equals').attr('onclick', ''); | |
| } else { | |
| $('#operation').val($('#operation').val() + $('#answer').val() + $('#divide').val()); | |
| $('#answer').val(''); | |
| $('#display').val($('#display').val() + $(this).val()); | |
| $('#equals').attr('onclick', ''); | |
| } | |
| }); | |
| // Product Button | |
| $('#product').click(function(e) { | |
| if ($('#answer').val() === '') { | |
| $('#equals').attr('onclick', ''); | |
| return false; | |
| } else if ($('#operation').attr('class') == 'activeAnswer') { | |
| $('#operation').val($('#operation').val() + $('#product').val()); | |
| $('#answer').val(''); | |
| $('#display').val($('#display').val() + $(this).val()); | |
| $('#equals').attr('onclick', ''); | |
| } else { | |
| $('#operation').val($('#operation').val() + $('#answer').val() + $('#product').val()); | |
| $('#answer').val(''); | |
| $('#display').val($('#display').val() + $(this).val()); | |
| $('#equals').attr('onclick', ''); | |
| } | |
| }); | |
| // Equals Button | |
| $('#equals').click(function() { | |
| 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)); | |
| $('#display').val($('#display').val() + $(this).val() + $('#answer').val()); | |
| $('#operation').addClass('activeAnswer'); | |
| $('#equals').attr('onclick', 'return false'); | |
| } | |
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <script src="//cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #calculator { | |
| margin-top: 10px; | |
| height: 435px; | |
| background: #d4d4d4; | |
| position: relative; | |
| min-width: 370px; | |
| border-radius: 10px; | |
| } | |
| .button-container { | |
| padding-left: 80px; | |
| } | |
| #title {} | |
| #answer { | |
| height: 50px; | |
| width: 220px; | |
| position: relative; | |
| background: #e3e3e3; | |
| margin: 0px auto 0px 60px; | |
| text-align: right; | |
| font-size: 26px; | |
| } | |
| #display { | |
| height: 20px; | |
| width: 220px; | |
| position: relative; | |
| background: #e3e3e3; | |
| margin: 0px auto 10px 60px; | |
| text-align: right; | |
| font-size: 10px; | |
| } | |
| .well { | |
| padding: 5px !important; | |
| /* Bootstrap Override */ | |
| margin-bottom: 5px; | |
| /* Bootstrap Override */ | |
| } | |
| .empty { | |
| position: relative; | |
| top: 11px !important; | |
| } | |
| button { | |
| width: 50px; | |
| height: 50px; | |
| border-radius: 5px; | |
| font-size: 24px; | |
| /*margin: 0px 10px 0 10px;*/ | |
| } | |
| .tall-button { | |
| float: right; | |
| height: 105px; | |
| margin-right: 78px; | |
| } | |
| .bottom-row { | |
| padding-left: 15px; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <link href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css" rel="stylesheet" /> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment