473,406 Members | 2,549 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,406 software developers and data experts.

For loop problem

132 100+
Hi,

I'm making a script where I need a for loop to get the values out of selects and do a function on it. Except I don't know how to get those values out if there because the id of the selects are all different.

Here's a piece of the code.

Expand|Select|Wrap|Line Numbers
  1. <td>
  2. <select name="team1">
  3. <option value="1">1</option>
  4. <option value="2">2</option>
  5. <option value="3">3</option>
  6. </select>
  7. </td>
  8. <td><input type="text" name="score1" size="3"> - <input type="text" name="score2" size="3"></td>
  9. <td>
  10. <select name="team2">
  11. <option value="1">1</option>
  12. <option value="2">2</option>
  13. <option value="3">3</option>
  14. </select>
  15. </td>
  16. </tr>
  17. <tr>
  18. <td>
  19. <select name="team3">
  20. <option value="1">1</option>
  21. <option value="2">2</option>
  22. <option value="3">3</option>
  23. </select>
  24. </td>
  25. <td><input type="text" name="score3" size="3"> - <input type="text" name="score4" size="3"></td>
  26. <td>
  27. <select name="team4">
  28. <option value="1">1</option>
  29. <option value="2">2</option>
  30. <option value="3">3</option>
  31. </select>
  32. </td>
  33. </tr>
  34.  
And this is a part of the function:
Expand|Select|Wrap|Line Numbers
  1. for (var i=0; i<stand.length; i++)
  2. {
  3. if (document.klassement.team1.value == stand[i].name)
  4. {
  5. for (var k=0; k<stand.length; k++)
  6. {
  7. if (document.klassement.team2.value == stand[k].name)
  8. {
  9. stand[i].goalsm = parseInt(stand[i].goalsm) + parseInt(document.klassement.score11.value);
  10. stand[k].goalsr = parseInt(stand[k].goalsr) + parseInt(document.klassement.score11.value);
  11. stand[i].goalsr = parseInt(stand[i].goalsr) + parseInt(document.klassement.score12.value);
  12. stand[k].goalsm = parseInt(stand[k].goalsm) + parseInt(document.klassement.score12.value);
  13.  
  14. if (document.klassement.score11.value < document.klassement.score12.value)
  15. {
  16. stand[k].points = parseInt(stand[k].points) +3;
  17. stand[k].won = parseInt(stand[k].won) +1;
  18. }
  19.  
  20. else if (document.klassement.score11.value > document.klassement.score12.value)
  21. {
  22. stand[i].points = parseInt(stand[i].points) +3;
  23. stand[i].won = parseInt(stand[i].won) +1;
  24. }
  25.  
  26. else if (document.klassement.score11.value == document.klassement.score12.value)
  27. {
  28. stand[i].points = parseInt(stand[i].points) +1;
  29. stand[k].points = parseInt(stand[k].points) +1;
  30. }
  31. }
  32. }    
  33. }
  34. }
  35.  
Now the problem is that I'm executing this function on the first row of the table only, so I'm using team1, team2, score1 and score2.

But there are 8 more rows that I need to execute this function onto.
So I need to do the same for team3, team4, score3 and score4 and again for team5, team6, score5 and score6 and so on until I have done this function 9 times.

Now I could paste the entire function 9 times and just change the names manually but it's probably much easier to use a for loop to repeat it 9 times but I don't know how I could change my names.

So any help on this would be appreciated.

Greets,

Kenneth
Aug 21 '08 #1
3 1156
acoder
16,027 Expert Mod 8TB
Use the elements array, e.g.
Expand|Select|Wrap|Line Numbers
  1. document.forms["klassement"].elements["team"+i].value
Aug 21 '08 #2
Cainnech
132 100+
Indeed this solution works perfectly,

Thank you!
Aug 23 '08 #3
acoder
16,027 Expert Mod 8TB
You're welcome. Glad it worked :)
Aug 23 '08 #4

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

Similar topics

0
by: Charles Alexander | last post by:
Hello I am new to php & MySQL - I am trying to retrieve some records from a MySQL table and redisplay them. The data in list form looks like this: Sample_ID Marker_ID Variation ...
5
by: build | last post by:
G'day All, I have a problem with this loop. There are a number of .txt files in 'myPath'. tmpFile = Dir(myPath & "\*.txt") 'PROCESS FOLDER Do Until tmpFile = "" <lottsa code> <too much to...
11
by: ritterhaus | last post by:
Just a simple bit of code to toggle between two state at intervals... import time for i in range(4): print 'On' time.sleep(1) print 'Off' time.sleep(1) .... SHOULD toggle On and Off four...
12
by: reynoldscraigr | last post by:
Hi All, hope someone can see what wrong here I have the following function function RemoveMenuFromHoldArray(menuName) { var i = 0; for (i=0;i<=MenusToHoldOpen.length-1;i++) { if...
43
by: Gremlin | last post by:
If you are not familiar with the halting problem, I will not go into it in detail but it states that it is impossible to write a program that can tell if a loop is infinite or not. This is a...
63
by: Aaron Ackerman | last post by:
What is the sytax for exiting a for loop in C#?
15
by: Mike Lansdaal | last post by:
I came across a reference on a web site (http://www.personalmicrocosms.com/html/dotnettips.html#richtextbox_lines ) that said to speed up access to a rich text box's lines that you needed to use a...
2
by: d3vkit | last post by:
Okay so I can NOT get my while loop to work. It's the most confusing thing I've ever come across. It was working fine and then suddenly, nothing. No error. The page just dies. I am using PHP5 with...
5
by: sgurukrupagmailcom | last post by:
Hi, I haven't come accross an elegant solution to a design problem that I show below. Have a look at the piece of code here: class Exc { Exc () { System.out.println ("Haribol"); }
1
by: JavaJon | last post by:
Hello, I'm Jon. I've recently picked up Java after using a "gimmick" programming language called GML ( Game Maker Language ). I've read a lot of tutorials and even a Java for Dummies *.pdf book....
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.