Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
jQuery - Calculate inputs
var week = $('.week');
var month= $('.month');
var year = $('.year');
function weekCalc(sender, args) {
var value = sender.val();
var milesPerMonth = value * 4.35;
var milesPerYear = value * 52;
month.val(Math.round(milesPerMonth));
year.val(Math.round(milesPerYear));
}
function monthCalc(sender, args) {
var value = sender.val();
var milesPerWeek = value / 4.35;
var milesPerYear = value * 12;
week.val(Math.round(milesPerWeek));
year.val(Math.round(milesPerYear));
}
function yearCalc(sender, args) {
var value = sender.val();
var milesPerWeek = value / 52;
var milesPerMonth = value / 12;
week.val(Math.round(milesPerWeek));
month.val(Math.round(milesPerMonth));
}
week.change(function () {
weekCalc($(this));
});
week.keyup(function () {
weekCalc($(this));
}).keyup();
month.change(function () {
monthCalc($(this));
});
month.keyup(function () {
monthCalc($(this));
}).keyup();
year.change(function () {
yearCalc($(this));
});
year.keyup(function () {
yearCalc($(this));
}).keyup();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment