radio or checkbox

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • colinod
    Contributor
    • Nov 2007
    • 347

    radio or checkbox

    I have the following code that adds up amounts if checked using a checkbox

    Code:
    <script type="text/javascript">
    function calculate(f)
    {
    var nums = f.num;
    var ntext = f.num;
    var result = 0;
    for(var i=0;i<nums.length;i++)
    {
    if(nums[i].checked)
    {
    result+=parseFloat(ntext[i].value);
    }
    }
    f.answer.value=Number(result).toFixed(2);
    }
    </script>
    it can be seen here http://www.yaketyyakallmouth.com/rad...enew/index.asp

    problem is when you have the two selections next to each other we really only want one to be selected so i thought of using radio selectors instead but they use the name to group them together and this is used by the code above to add the amounts together.

    any ideas around this
  • Dormilich
    Recognized Expert Expert
    • Aug 2008
    • 8694

    #2
    jepp, using events on each radio that de/increments the value. see addAmount()

    Comment

    • colinod
      Contributor
      • Nov 2007
      • 347

      #3
      i found out that if i changed the id tag in the radio button to num and used the name as a field in the database that groups the stations together that works.

      Comment

      • Dormilich
        Recognized Expert Expert
        • Aug 2008
        • 8694

        #4
        although it may work, it is dead wrong from the HTML point-of-view. since ids must be unique, you cannot substitute id for name. additionally, you can’t deselect a radio group, once it has been checked.

        try getElementsByTa gName() or getElementsByCl assName()

        Comment

        • colinod
          Contributor
          • Nov 2007
          • 347

          #5
          i dont see what effect that will have if the page works?

          and as i am useless with javascript i dont know what to do?

          Comment

          • Dormilich
            Recognized Expert Expert
            • Aug 2008
            • 8694

            #6
            crossing a red traffic light does work too, until you get run over by a car.

            give all the radios/checkboxes that should be calculated a common class name. employing the method document.getEle mentsByClassNam e() you can loop through that list to add the values.

            the other approach is the incremental addition/subtraction of the currently clicked radio/checkbox (see your other thread). it only requires the events to be attached to every radio/checkbox, which can be accomplished in a similar manner.

            Comment

            • colinod
              Contributor
              • Nov 2007
              • 347

              #7
              problem i have is my lack of knowledge, the javascript i am using was not written by me just found it on the internet so am not sure how to change it to use document.getEle mentsByClassNam e() etc.

              Comment

              • colinod
                Contributor
                • Nov 2007
                • 347

                #8
                I am also trying to reset both the form and the total, i can reset the form but it only sets it back to the total carried over from another page? and if i reset the total as soon as i click another checkbox it adds that to the total carried over too

                The javascript for keeping the totals is now with the result coming from the previouse pages total answer

                Code:
                <script type="text/javascript">
                function calculate(f)
                {
                var nums = f.num;
                var ntext = f.num;
                var result = <%= request.form("answer")%>;
                for(var i=0;i<nums.length;i++)
                {
                if(nums[i].checked)
                {
                result+=parseFloat(ntext[i].value);
                }
                }
                f.answer.value=Number(result).toFixed(2);
                 }
                </script>
                and the code for zeroing the total is

                Code:
                <SCRIPT language="javascript">
                function setzero(f)
                {
                var result = 0;
                document.getElementById("total").value = Number(result).toFixed(2);
                }
                </SCRIPT>
                this works but only zeros the total in the text element of the form with the id "total" as soon as i add anything again its wrong. i am sure this is because of the request in asp in the script above but cant figure it out

                Comment

                • Dormilich
                  Recognized Expert Expert
                  • Aug 2008
                  • 8694

                  #9
                  does that mean you didn’t learn anything from your previous threads?
                  this works but only zeros the total in the text element of the form with the id "total" as soon as i add anything again its wrong.
                  obviously, the result variable set by ASP is still unchanged (and you always add on this)

                  Comment

                  • colinod
                    Contributor
                    • Nov 2007
                    • 347

                    #10
                    i have tried but cant always get my head around it

                    Comment

                    • Dormilich
                      Recognized Expert Expert
                      • Aug 2008
                      • 8694

                      #11
                      with the current design of calculate() you won’t solve that problem.

                      Comment

                      • colinod
                        Contributor
                        • Nov 2007
                        • 347

                        #12
                        any ideas on where to try? or is this down to me trying to figure out the document.getEle mentById way of doing this

                        Comment

                        • Dormilich
                          Recognized Expert Expert
                          • Aug 2008
                          • 8694

                          #13
                          it has nothing to do with getElementById( ). check out your calculate() function. every time you call it, it sums all checked values on top of the ASP value. clearing the text field to zero does not affect this in any way.

                          Comment

                          • colinod
                            Contributor
                            • Nov 2007
                            • 347

                            #14
                            hi i am trying to set the variable that is from the asp outside the function, i gather that should be the way to go but i keep getting NaN as a result in my text field not sure what this means

                            Comment

                            • colinod
                              Contributor
                              • Nov 2007
                              • 347

                              #15
                              Hi i now have script to do the calculation like this

                              Code:
                              <script type="text/javascript">
                              function UpdateCost() {
                                var sum = document.write("<%= runtotal %>");
                                var gn, elem;
                                for (i=0; i<5; i++) {
                                  gn = 'id'+i;
                                  elem = document.getElementById(gn);
                                  if (elem.checked == true) { sum += Number(elem.value); }
                                }
                                document.getElementById('total').value = sum.toFixed(2);
                              } 
                              </script>
                              runtotal is got from asp from the form of the the previous page and that works

                              my form element is this

                              Code:
                              <INPUT  type="checkbox" name="num" id="id<%= stationcount %>" onClick="UpdateCost()" value="<%= nationalRecordset("amount") %>">
                              station count is incremented through my asp loop from database

                              the problem i have at the moment is that when i go from one page to the page i am trying this on and look at the code the sum has the right total but when i click on the heckbox it reloads the page and just writes the sum on a blank page

                              Comment

                              Working...