Pen Settings

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

You're using npm packages, so we've auto-selected Babel for you here, which we require to process imports and make it all work. If you need to use a different JavaScript preprocessor, remove the packages in the npm tab.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Use npm Packages

We can make npm packages available for you to use in your JavaScript. We use webpack to prepare them and make them available to import. We'll also process your JavaScript with Babel.

⚠️ This feature can only be used by logged in users.

Code Indentation

     

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

HTML Settings

Here you can Sed posuere consectetur est at lobortis. Donec ullamcorper nulla non metus auctor fringilla. Maecenas sed diam eget risus varius blandit sit amet non magna. Donec id elit non mi porta gravida at eget metus. Praesent commodo cursus magna, vel scelerisque nisl consectetur et.

            
              <html>

<head>
   <title>JS Calculator</title>
   <meta charset="utf-8">
   <link rel="stylesheet" href="calculator.css" />
</head>

<body>
   <div class="container-fluid">
      <div class="row">
         <div class="col-md-offset-3 col-md-6">
            <div id="main">
               <div class="top">
                  <span value="c" class="operator">c</span>
                  <div id="textbox">JS Calculator</div>
               </div>
               <div class="keys">
                  <span value="7">7</span>
                  <span value="8">8</span>
                  <span value="9">9</span>
                  <span class="operator" value="+">+</span>
                  <span value="4">4</span>
                  <span value="5">5</span>
                  <span value="6">6</span>
                  <span class="operator" value="-">-</span>
                  <span value="1">1</span>
                  <span value="2">2</span>
                  <span value="3">3</span>
                  <span class="operator" value="*">x</span>
                  <span value="%"></span>
                  <span value="0">0</span>
                  <span value=".">.</span>
                  <span class="operator" value="/">/</span>
                  <span id="equal" value="=">=</span>
               </div>
            </div>
         </div>
      </div>
   </div>
</body>

</html>
            
          
!
            
              html {
   height: 100%;
}

body {
   display: flex;
   justify-content: center;
   align-items: center;
   text-align: center;
   font-size: 20px;
   font-weight: bolder;
   color: black;
   font-family: "Verdana";
   background: white;
   background: radial-gradient(circle, #fff 20%, #ccc);
   background-size: cover;
}

#main {
   margin: 50px 0 0 0;
   width: 390px;
   height: 260px;
   background-color: #C9C5BA;
   padding: 10px;
   border-radius: 5px;
   box-shadow: 0px 4px #C9C5BA, 0px 10px 15px rgba(0, 0, 0, 0.2);
}

.keys span {
   width: 80px;
   height: 30px;
   background-color: #698996;
   border-radius: 3px;
   cursor: pointer;
   float: left;
   margin: 5px;
}

.operator {
   background-color: #407076 !important;
   color: black;
}

.keys span:hover {
   background: #407076;
   box-shadow: 0px 4px #407076;
   color: white;
}

.operator:active {
   box-shadow: 0px 0px #97B1A6;
   top: 4px;
}

.operator:hover {
   background: #698996 !important;
   box-shadow: 0px 4px #698996 !important;
   color: white;
}


/* Simulating "pressed" effect on active state of the keys by removing the box-shadow and moving the keys down a bit */

.keys span:active {
   box-shadow: 0px 0px #6b54d3;
   top: 4px;
}

.top span {
   float: left;
   width: 90px;
   height: 30px;
   cursor: pointer;
   background-color: #EBBAB9 !important;
   margin: 0 10px 0 5px;
   border-radius: 3px;
   padding-top: 1px;
}

.top span:hover {
   background: red !important;
   box-shadow: 0px 4px red !important;
   color: white;
}


/* Simulating "pressed" effect on active state of the keys by removing the box-shadow and moving the keys down a bit */

.top span:active {
   box-shadow: 0px 0px #6b54d3;
   top: 4px;
}

#textbox {
   width: 250px;
   height: 30px;
   margin-bottom: 10px;
   float: left;
   background: rgba(0, 0, 0, 0.3);
   border-radius: 3px;
   box-shadow: inset 0px 4px rgba(0, 0, 0, 0.2);
   padding-top: 1px;
   color: white;
}

#equal {
   width: 350px;
   height: 30px;
   background-color: #407076;
   border-radius: 3px;
   color: black;
   cursor: pointer;
   float: left;
   margin: 5px;
}

#equal:hover {
   background: ;
   box-shadow: 0px 4px #698996 !important;
   color: white;
}

#equal:active {
   box-shadow: 0px 0px #698996 !important;
   top: 4px;
}
            
          
!
            
              //Create variable for the digit buttons and the operators
var digitButtons = document.getElementsByTagName("span");
var result = "";
var endResult = 0;
//create onclick event to all digitButtons
for (var i = 0; i < digitButtons.length; i++) {
   digitButtons[i].addEventListener("click", clickHandler)
}

function clickHandler() {

   var input = this.getAttribute("value");
   var screen = document.getElementById("textbox");

   if (input != "=" && input != "c") {
      result += input;
      screen.innerHTML = result;
   } else if (input == "c") {
      result = "";
      screen.innerHTML = "";
   } else if (input == "=") {

      endResult = parseFloat(eval(result).toPrecision(8));
      screen.innerHTML = endResult;
      result = "";
   }

}
            
          
!
999px
🕑 One or more of the npm packages you are using needs to be built. You're the first person to ever need it! We're building it right now and your preview will start updating again when it's ready.

Console