Created
March 11, 2017 02:36
-
-
Save WEB24H/5be1bad3563f9e78c157f116b5b2d002 to your computer and use it in GitHub Desktop.
Real Time Form Input Javascript Calculator
This file contains 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
<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> |
This file contains 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
$('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