By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,528 Members | 1,324 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,528 IT Pros & Developers. It's quick & easy.

If Else statement

100+
P: 347
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
Expand|Select|Wrap|Line Numbers
  1. <TABLE width="90%" border="0" align="center" cellpadding="0" cellspacing="0">
  2.                    <TR>
  3.                      <TD width="50%"><DIV align="right">Total&nbsp;£
  4.  
  5.                          <INPUT name="answer" type="text" id="answer" style="border: none" value="" size="5" readonly="readonly"/>
  6.                      </DIV></TD>
  7.                      <TD width="50%"><DIV align="right">Total&nbsp;£
  8.  
  9.                        <INPUT name="answer2" type="text" id="answer2" style="border: none" value="" size="5" readonly="readonly"/>
  10.                      </DIV></TD>
  11.                    </TR>
  12.                  </TABLE>
and this is the javascript that keeps the totals with an onclick in the checkbox
Expand|Select|Wrap|Line Numbers
  1. <script type="text/javascript">
  2. function calculate(f)
  3. {
  4. var nums = f.num;
  5. var ntext = f.num;
  6. var result = 0;
  7. for(var i=0;i<nums.length;i++)
  8. {
  9. if(nums[i].checked)
  10. {
  11. result+=parseFloat(ntext[i].value);
  12. }
  13. }
  14. f.answer.value=Number(result).toFixed(2);
  15. }
  16. </script>
  17. <script type="text/javascript">
  18. function calculate2(f)
  19. {
  20. var nums2 = f.num2;
  21. var ntext2 = f.num2;
  22. var result2 = 0;
  23. for(var i=0;i<nums2.length;i++)
  24. {
  25. if(nums2[i].checked)
  26. {
  27. result2+=parseFloat(ntext2[i].value);
  28. }
  29. }
  30. f.answer2.value=Number(result2).toFixed(2);
  31. }
  32. </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
Dec 8 '09 #1
Share this Question
Share on Google+
3 Replies


Dormilich
Expert Mod 5K+
P: 8,639
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.
Expand|Select|Wrap|Line Numbers
  1. function enough()
  2. {
  3.     // using a random treshold of 1,000
  4.     if (1000 < this.value)
  5.     {
  6.         // change the style of the element to hide
  7.         document.getElementById(…).style.visibility = "hidden";
  8.     }
  9. }
  10. document.getElementById("answer").addEventListener("change", enough, false);
  11. // you can also use addEvent() to be IE-compatible
Dec 8 '09 #2

100+
P: 347
cant get this to work get an error
'document.getElementById(...)' is null or not an object

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

any ideas
Dec 11 '09 #3

Dormilich
Expert Mod 5K+
P: 8,639
'document.getElementById(...)' 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 addEventListener(), you can’t accomplish the same with inline JavaScript (like <a … onclick="myfunc()">).
Dec 12 '09 #4

Post your reply

Sign in to post your reply or Sign up for a free account.