Pen Settings

HTML

CSS

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

JavaScript

Babel includes JSX processing.

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

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

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.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <div class="d-flex justify-content-center">
  <div class="my-3 border p-3">
    <h2 class="text-center" > Tip Calculator </h2>
    <form>
      <div class="form-group">
    <label for="amount">Bill amount:</label>
    <input type="text" class="form-control" placeholder="Enter amount" id="bill_amount">
      </div>
      <span><b> Tax percent: </b></span> <span id="tax_percent">7.25%</span> <br/>
      <span><b> Tax amount: </b></span> $<span id="tax_amt">0.00</span> <br/> <br/>
      <div class="btn-group">
        <button type="button" class="btn btn-primary tip_btn active" id="12_btn">12%</button>
        <button type="button" class="btn btn-primary tip_btn" id="15_btn">15%</button>
        <button type="button" class="btn btn-primary tip_btn" id="18_btn">18%</button>
        <button type="button" class="btn btn-primary tip_btn" id="custom_btn">Custom Tip %</button>
      </div> </br> 
      <div class="form-group" id="custom_div" style="display: none;">
        </br>
        <input type="text" class="form-control" placeholder="Enter custom tip percentage" id="custom_tip_input">
      </div>
  </br>
    </form>
    <span><b> Tip: </b></span> $<span id="tip">0.00</span> <br/>
    <span><b> Total: </b></span> $<span id="total">0.00</span> <br/>
    </br>
    <button type="button" class="btn btn-primary" id="split_btn">Split</button>
    </br>
    <div id="split_info">
      </br> 
      <input type="text" class="form-control" placeholder="Enter number of diners" id="split_count_input"> </br>
      <span><b> Tip per head: </b></span> $<span id="tip_per">0.00</span> <br/>
      <span><b> Total per head: </b></span> $<span id="total_per">0.00</span> <br/>
    </div>
  </div>
</div>
              
            
!

CSS

              
                #split_btn {
  width: 100%;
}

#split_info {
  display: none;
}
              
            
!

JS

              
                $(document).ready(function() {
  var tip_percent = 0.12;
  var tax_percent = 0.0725;
  var num_diners = 1;
  var bill_amt = 0;
  var tip = 0;
  var total = 0;
  function update() {
    tip = bill_amt * tip_percent
    total = bill_amt * (1 + tip_percent + tax_percent)
    $("#tax_amt").html((bill_amt * tax_percent).toFixed(2));
    $("#tip").html(tip.toFixed(2));
    $("#total").html(total.toFixed(2));
    $("#tip_per").html((tip/num_diners).toFixed(2));
    $("#total_per").html((total/num_diners).toFixed(2));
  }
 
  
  update();
  
  
  
  $("#bill_amount").on('input', function() {
    
    var str_retrieved = $(this).val();

    if (isNaN(str_retrieved) || str_retrieved == '') {
      bill_amt = 0;
    } else {
      
      bill_amt = parseFloat(str_retrieved);
    }
   
    update();
  });
  
  $("#split_count_input").on('input', function() {
    var str_retrieved = $(this).val();
    
    if (isNaN(str_retrieved) || str_retrieved == '') {
      num_diners = 1;
      //alert("1");
    } else {
      var num = parseFloat(str_retrieved);
      if (!Number.isInteger(num)) {
        num_diners = 1;
        //alert("2");
      } else {
        if (num == 0) {
          num_diners = 1;
          //alert("3");
        } else {
          num_diners = num;
        }
      } 
    }
   
    update();
  });
  
  $("#custom_tip_input").on('input', function() {
    var str_retrieved = $(this).val();

    if (isNaN(str_retrieved) || str_retrieved == '') {
      tip_percent = 0;
    } else {
      
      tip_percent = parseFloat(str_retrieved)/100;
    }
   
    update();
  });
  
  $("#custom_btn").click(function() {
    $(".tip_btn").removeClass("active");
    $(this).addClass("active");
    $("#custom_div").css("display", "inline");
    tip_percent = 0;
    update();
  });
  
  $("#12_btn").click(function() {
    $(".tip_btn").removeClass("active");
    $(this).addClass("active");
    $("#custom_div").css("display", "none");
    tip_percent = 0.12;
    update();
  });
  
  $("#15_btn").click(function() {
    $(".tip_btn").removeClass("active");
    $(this).addClass("active");
    $("#custom_div").css("display", "none");
    tip_percent = 0.15;
    update();
  });
  
  $("#18_btn").click(function() {
    $(".tip_btn").removeClass("active");
    $(this).addClass("active");
    $("#custom_div").css("display", "none");
    tip_percent = 0.18;
    update();
  });
  
  $("#split_btn").click(
    function() {
      $(this).toggleClass("btn-danger btn-primary");
      $("#split_info").toggle(400);
      
      // toggle description of button
      var text = $(this).text();
      if (text == "Split") {
        $(this).text("Unsplit");
      } else {
        $(this).text("Split");
      }
    }           
                     
  );
  
});
              
            
!
999px

Console