SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Enthusiast
    Join Date
    Sep 2007
    Posts
    36
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    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

  2. #2
    SitePoint Addict
    Join Date
    Oct 2009
    Location
    London, UK
    Posts
    382
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    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.

    Code:
    $(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.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •