If Else statement

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

    If Else statement

    I have a page that adds up amounts assigned to checkboxes in a form and gives a total at the bottom of the page, this is done with some javascript i found as i am useless with it.

    What i am tring to do is change the part of the page that is visible if the amount of the total gets above a certain amount.

    this is the code that shows the totals
    Code:
    <TABLE width="90%" border="0" align="center" cellpadding="0" cellspacing="0">
                       <TR>
                         <TD width="50%"><DIV align="right">Total&nbsp;£
    
                             <INPUT name="answer" type="text" id="answer" style="border: none" value="" size="5" readonly="readonly"/>
                         </DIV></TD>
                         <TD width="50%"><DIV align="right">Total&nbsp;£
    
                           <INPUT name="answer2" type="text" id="answer2" style="border: none" value="" size="5" readonly="readonly"/>
                         </DIV></TD>
                       </TR>
                     </TABLE>
    and this is the javascript that keeps the totals with an onclick in the 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>
    <script type="text/javascript">
    function calculate2(f)
    {
    var nums2 = f.num2;
    var ntext2 = f.num2;
    var result2 = 0;
    for(var i=0;i<nums2.length;i++)
    {
    if(nums2[i].checked)
    {
    result2+=parseFloat(ntext2[i].value);
    }
    }
    f.answer2.value=Number(result2).toFixed(2);
    }
    </script>
    I am able to make something like this work with asp but dont really want to have to reload the page on every click

    can anyone help

    the page can be found at http://www.yaketyyakallmouth.com/rad...new/local2.asp
  • Dormilich
    Recognized Expert Expert
    • Aug 2008
    • 8694

    #2
    that page looks familiar………

    for your HTML I recommend a six column table … much easier to read.

    the idea behind the hiding is quite simple. if the condition is met (value of totals field > treshold) change CSS of the element.
    Code:
    function enough()
    {
        // using a random treshold of 1,000
        if (1000 < this.value)
        {
            // change the style of the element to hide
            document.getElementById(…).style.visibility = "hidden";
        }
    }
    document.getElementById("answer").addEventListener("change", enough, false);
    // you can also use addEvent() to be IE-compatible

    Comment

    • colinod
      Contributor
      • Nov 2007
      • 347

      #3
      cant get this to work get an error
      'document.getEl ementById(...)' is null or not an object

      i have replaced the (...) with the id of the text i want to change to ("text")

      any ideas

      Comment

      • Dormilich
        Recognized Expert Expert
        • Aug 2008
        • 8694

        #4
        'document.getEl ementById(...)' is null or not an object
        obviously…

        since I’m not familiar with your source code (I take a look at it only from time to time) you have to add the appropriate id values yourself.

        further, there is a good reason I use addEventListene r(), you can’t accomplish the same with inline JavaScript (like <a … onclick="myfunc ()">).

        Comment

        Working...