Jquery calculator

hi

Im look for help in building an expenditure calculator

what i am having problems with is I am using radio buttons to multiply form field values

[radio1] = x 1 (week)
[radio2] = x 4 (month)
[radio3] = x 52 (year)

So if you fill out a field and select [radio2] it multiplies the field by 4

If you then select [radio1] it reverts to the original value

Using JQuery what’s the best way to do this.

The issue I am having is I need to know the previous selected radio as well as the new one so I know whether to divide or multiply

Apologies, but I’m not great with javaScript. I tend to use the jQuery library to make my life easier. Here’s what I’d do with the jQuery api.


$(document).ready(function(){
  $('radio').click(function(){
    var ratio = $(this).val();  //get value of radio button clicked
    var fieldVal = $('#formField').val();  //get value of form field
    var newVal = ratio * fieldVal;  //multiply
    $('#formField').val(newVal);  //change the form field's value to the result of the multiplication
  });
});

If you’re not using jQuery perhaps it could serve as pseudo-code…? Anyway, I hope that helps.