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

How to do reverse value alert using for statement

P: 27
Hi,

Could someone advise me how to do the reverse values in this code? 300, 200, 100. Think I'm really hopeless when come to programming :-(

for (intIndex = 0; intIndex > ayrElements.length ;intIndex--)// this is the part that I'm stuck.

Here's the code:

Expand|Select|Wrap|Line Numbers
  1. <html>
  2. <head>
  3. <style>
  4. #divMessage { width : 500px;
  5.     height : 30px;
  6.     border : 1px solid black;
  7.     }
  8.  
  9. body { font : arial;
  10.     font-size : 10px;}
  11.  
  12. </style>
  13. <script language="javascript">
  14.  
  15.  
  16.   function inspectCheckBoxes(){    
  17.  
  18.    var aryElements = new Array(); 
  19.    var intValue = new Number(0); 
  20.    var intIndex  = new Number(0); 
  21.  
  22.      aryElements = document.getElementsByName("chkData");
  23.  
  24.    for (intIndex= 0 ;intIndex <aryElements.length;intIndex++){
  25.     if (aryElements[intIndex].checked)
  26.                alert(aryElements[intIndex].value);
  27.    }
  28. }
  29. </script>
  30. </head>
  31. <body>
  32. <table border="1">
  33.   <tr>
  34.    <td colspan="2">
  35.     <b>Experiment 1</b><br>
  36.     Experiment using JavaScript to communicate and 
  37.     control the checkbox controls.</td>
  38.   </tr>
  39.   <tr>
  40.     <td style="width:10%"><input type="checkbox" name="chkData" value="100"/></td>
  41.     <td>100</td>
  42.   </tr>
  43. <tr>
  44.     <td><input type="checkbox" name="chkData" value="200"/></td>
  45.     <td>200</td>
  46.   </tr>
  47.   <tr>
  48.     <td><input type="checkbox" name="chkData" value="300"/></td>
  49.     <td>300</td>
  50.   </tr>
  51.   <tr>
  52.      <td colspan="2">
  53.      <div id="divMessage" ></div>
  54.      </td>
  55.   </tr> 
  56.   <tr>
  57.     <td colspan="2" align="right">
  58.      <input type="button" id="btnExperiment" value="Experiment" onclick="inspectCheckBoxes()" >
  59.     </td>
  60.   </tr>
  61. </table>
  62. </body>
  63. </html>
Jan 19 '10 #1
Share this Question
Share on Google+
7 Replies


Dormilich
Expert Mod 5K+
P: 8,639
for (intIndex = 0; intIndex > ayrElements.length ;intIndex--)
letís apply some common logic:

if you count down, do it from the largest value to the smallest value.

counting down from 0 will get you in negative numbers Ö
quitting the loop when the counter variable is above the maximum value Ö is a condition you never reach with decrementing

try it, it should be easier now.
Jan 19 '10 #2

P: 27
Hi,

It doesn't work. I run it and nothing happens.

By right, it should give me alert message of 300, 200, 100.

:-(
Jan 19 '10 #3

Plater
Expert 5K+
P: 7,872
Well even when you do understand how for loops work (http://www.javascriptkit.com/javatutors/loop3.shtml), is the "collection of objects" returned by getElementsByName() garunteed to be in the same order all the time?
Jan 19 '10 #4

Dormilich
Expert Mod 5K+
P: 8,639
let me guess Ö you didnít select any checkbox?
Jan 19 '10 #5

Dormilich
Expert Mod 5K+
P: 8,639
Well even when you do understand how for loops work, is the array returned by getElementsByName() garunteed to be in the same order all the time?
a) itís not an Array
b) NodeLists are in document order

EDIT: some thougths on the code
Expand|Select|Wrap|Line Numbers
  1. function inspectCheckBoxes(){    
  2.    // unnecessary, aryElementsí type will be set later
  3.    var aryElements = new Array(); 
  4.    // not used, why define?
  5.    var intValue = new Number(0); 
  6.    // var intIndex = 0 would have been sufficient
  7.    var intIndex  = new Number(0); 
  8.  
  9.    // declare the variable here: var elements = documentÖ
  10.      aryElements = document.getElementsByName("chkData");
  11.  
  12.    // avoid calculations in the abort condition part, i.e. calculate the length only once:
  13.    // for (var i=0, l<elements.length; i<l, i++)
  14.    for (intIndex= 0 ;intIndex <aryElements.length;intIndex++){
  15.     // itís good coding practice to wrap every code block of if in { } even if it is only one line
  16.     if (aryElements[intIndex].checked)
  17.                alert(aryElements[intIndex].value);
  18.    }
  19. }
Jan 19 '10 #6

Dormilich
Expert Mod 5K+
P: 8,639
after dorm's nitpicking:
is the "collection of objects" returned by getElementsByName() garunteed to be in the same order all the time?
yes, it is.
Jan 19 '10 #7

P: 27
I have found the solution already.

Just use :-
Expand|Select|Wrap|Line Numbers
  1. for(intIndex=aryElements.length-1; intIndex>=0; intIndex--){
  2. alert(aryElements[intIndex].value)
  3. }
Jan 20 '10 #8

Post your reply

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