Skip to content

Instantly share code, notes, and snippets.

@WEB24H
Created March 11, 2017 02:36
Show Gist options
  • Save WEB24H/5be1bad3563f9e78c157f116b5b2d002 to your computer and use it in GitHub Desktop.
Save WEB24H/5be1bad3563f9e78c157f116b5b2d002 to your computer and use it in GitHub Desktop.
Real Time Form Input Javascript Calculator
<input id='first' type="text" class="form-control formBlock" name="bus_ticket" placeholder="Bus Ticket..." required/><br />
<input id='second' type="text" class="form-control formBlock" name="plane_ticket" placeholder="Plane Ticket..." required/><br />
<input id='third' type="text" class="form-control formBlock" name="hotel_expenses" placeholder="Hotel Expenses..." required/><br />
<input id='fourth' type="text" class="form-control formBlock" name="eating_expenses" placeholder="Eating Expenses..." required/><br />
Total : <span id="total_expenses"></span>
$('input').keyup(function(){ // run anytime the value changes
var firstValue = parseFloat($('#first').val()); // get value of field
var secondValue = parseFloat($('#second').val()); // convert it to a float
var thirdValue = parseFloat($('#third').val());
var fourthValue = parseFloat($('#fourth').val());
$('#total_expenses').html(firstValue + secondValue + thirdValue + fourthValue); // add them and output it
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment