Created
March 11, 2017 02:57
-
-
Save WEB24H/db28f6faa09a791928c570d88a3f89c6 to your computer and use it in GitHub Desktop.
Real Time Javascript Calculator For HTML Inputs
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 class="input" id='first' type="text" class="form-control formBlock" name="bus_ticket" placeholder="Bus Ticket..." required/><br /> | |
<input class="input" id='second' type="text" class="form-control formBlock" name="plane_ticket" placeholder="Plane Ticket..." required/><br /> | |
<input class="input" id='third' type="text" class="form-control formBlock" name="hotel_expenses" placeholder="Hotel Expenses..." required/><br /> | |
<input class="input" id='fourth' type="text" class="form-control formBlock" name="eating_expenses" placeholder="Eating Expenses..." required/><br /> | |
Total : <input id='total_expenses' type="text" class="form-control formBlock" name="funding" placeholder="Total Expenses..."/> |
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 = Number($('#first').val()); // get value of field | |
var secondValue = Number($('#second').val()); // convert it to a float | |
var thirdValue = Number($('#third').val()); | |
var fourthValue = Number($('#fourth').val()); | |
$('#total_expenses').val(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