Radio button string and math help needed

Discussion in 'HTML & Website Design' started by I_Basically_need_help, Dec 12, 2005.

  1. #1
    Radio button string and math help needed :)

    I will have numerous Radio buttons each selection will need to have a value that represents a cost related to that selection, my customer will need to see the update immediately after making his/her selection.

    In addition the change of value of each radio button must create a string that represents the final part number.

    1st part Cost: If I had three radio buttons, selected, I would need to sum the cost values and always update a field the customer can see, no buttons, just an update on the selection.

    2nd part String: Again the same three radio buttons, if button 1 string is “12” and button 2 string is “TE” and button 3 string is “0p” then I would need to display a value “12TE0p” this to would need to be on any radio box selection.

    Can some one help me with a small example?
     
    I_Basically_need_help, Dec 12, 2005 IP
  2. Sham

    Sham Peon

    Messages:
    136
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Hmm, interesting.

    You're probably better off using checkboxes, seeing as you cant select all 3 radio buttons even if you wanted to...

    The first thing that jumps out at me, is that you are going to need a hash array somewhere in your JS which contains the associated string for each checkbox.

    You will need another hash which indicates if that portion of text is to be included in the final string, probably by the presence of a value of 1



    i.e.

    
    <script language="javascript">
    
    var boxStringHash = new Array();
    boxStringHash['box1'] = "12";
    boxStringHash['box2'] = "TE";
    .... (and so on for all your check boxes)
    
    var finalString = new Array();
    
    var grandTotal=0;
    </script>
    
    Code (markup):
    Now, for your check boxes, you will need something like:

    
    <input type="checkbox" onchange="reCalc(this);" value="24.99" name="box1">
    <input type="checkbox" onchange="reCalc(this);" value="15.49" name="box2">
    
    Code (markup):
    And a JS method called reCalc like this (with a method to generate the string at the end):

    
    function reCalc(checkboxObject){
    
    if(checkboxObject.selected){
     // checkbox was checked
     finalString[checkboxObject.name]=1;
     // add to the total amount
     grandTotal+=parseInt(checkboxObject.value);
    
    }
    else{
     // checkbox was unchecked
     finalString[checkboxObject.name]=0;
     // remove from the total amount
     grandTotal+=parseInt(checkboxObject.value);
    }
    
    // display the generated string
    alert(generateFinalString());
    
    // display the new amount
    alert(grandTotal);
    
    }
    
    function generateFinalString(){
     var generatedString="";
    
     for(i=0;i<finalString.legth;i++){
      if(finalString[i]==1){
        generatedString+=finalString[i];
      }
     }
    
    return generatedString;  
    
    }
    
    
    Code (markup):
    This is probably riddled with bugs because I haven't got time to test it at the moment, but it I were you, I'd go from here...

    Gd Luck.
     
    Sham, Dec 13, 2005 IP
  3. I_Basically_need_help

    I_Basically_need_help Peon

    Messages:
    4
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Any help would be cool...

    This is where I am today "type=radio value=GW-,2.99 name"
    the red part I need to break or format some how?

    I got the rest of it figured out...

    On the bottom of the basic app I need to show the new part number, so i need to string the radio button values before the "," or len count and some trim function, I don't know i am to tiered.......

     
    I_Basically_need_help, Dec 13, 2005 IP