473,387 Members | 1,687 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

How to do reverse value alert using for statement

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
7 2334
Dormilich
8,658 Expert Mod 8TB
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
tangara
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
7,872 Expert 4TB
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
8,658 Expert Mod 8TB
let me guess … you didn’t select any checkbox?
Jan 19 '10 #5
Dormilich
8,658 Expert Mod 8TB
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
8,658 Expert Mod 8TB
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
tangara
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

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

Similar topics

1
by: G Kannan | last post by:
Hey all! I have written a perl script to retrieve information from a HTML Form and insert the data into an Oracle database table. I am gettting the the following error message: "Use of...
1
by: Mokoena | last post by:
I can't seem to do an if statement check on the value returned from a selected radio button (isexistingleague). The code never runs anything within the if statement that tests if "true" is...
7
by: Tony Cooke | last post by:
Hi all. I'm not sure why I'm having problems with this but if I try to retrieve the value of a readonly text form I get back that the object is undefined. The reason the text is readonly is...
8
by: Jim Langston | last post by:
I have a class I designed that stores text chat in a std::vector<sd::string>. This class has a few methods to retrieve these strings to be displayed on the screen. void ResetRead( bool Reverse,...
27
by: Abdullah Kauchali | last post by:
Hi folks, Can one rely on the order of keys inserted into an associative Javascript array? For example: var o = new Object(); o = "Adam"; o = "Eve";
14
by: ford_desperado | last post by:
Why isn't ALLOW REVERSE SCANS the default? Why do we have to - drop PK - create an index - recreate PK What are the advantages of indexes that do not allow reverse scans?
22
by: delraydog | last post by:
It's quite simple to walk to the DOM tree going forward however I can't figure out a nice clean way to walk the DOM tree in reverse. Checking previousSibling is not sufficient as the...
3
by: thrill5 | last post by:
I have an xml document such as: <device> <element>first</element> <element>second</element> </device> I am using this as the source for an xslt transform that goes like <xsl:for-each...
1
by: kang jia | last post by:
hi currently i am editing signup page, when user enter deupicated NRIC and click signup, they will go to do_signuppage and read the error message and then after 5 seconds, they will be redirected...
1
by: sunnyluthra1 | last post by:
Hi, I was creating an Application in MS Access for Geocoding a particular Address from Google to get the Lat & Long. I successfully able to did that. Here is the code:...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.