Skip to content

Instantly share code, notes, and snippets.

@kyranjamie
Created May 10, 2012 16:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kyranjamie/2654337 to your computer and use it in GitHub Desktop.
Save kyranjamie/2654337 to your computer and use it in GitHub Desktop.
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