For loop problem

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Cainnech
    New Member
    • Nov 2007
    • 132

    For loop problem

    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.

    Code:
    <td>
    <select name="team1">
    <option value="1">1</option>
    <option value="2">2</option>
    <option value="3">3</option>
    </select>
    </td>
    <td><input type="text" name="score1" size="3"> - <input type="text" name="score2" size="3"></td>
    <td>
    <select name="team2">
    <option value="1">1</option>
    <option value="2">2</option>
    <option value="3">3</option>
    </select>
    </td>
    </tr>
    <tr>
    <td>
    <select name="team3">
    <option value="1">1</option>
    <option value="2">2</option>
    <option value="3">3</option>
    </select>
    </td>
    <td><input type="text" name="score3" size="3"> - <input type="text" name="score4" size="3"></td>
    <td>
    <select name="team4">
    <option value="1">1</option>
    <option value="2">2</option>
    <option value="3">3</option>
    </select>
    </td>
    </tr>
    And this is a part of the function:
    Code:
    for (var i=0; i<stand.length; i++)
    {
    if (document.klassement.team1.value == stand[i].name)
    {
    for (var k=0; k<stand.length; k++)
    {
    if (document.klassement.team2.value == stand[k].name)
    {
    stand[i].goalsm = parseInt(stand[i].goalsm) + parseInt(document.klassement.score11.value);
    stand[k].goalsr = parseInt(stand[k].goalsr) + parseInt(document.klassement.score11.value);
    stand[i].goalsr = parseInt(stand[i].goalsr) + parseInt(document.klassement.score12.value);
    stand[k].goalsm = parseInt(stand[k].goalsm) + parseInt(document.klassement.score12.value);
    					
    if (document.klassement.score11.value < document.klassement.score12.value)
    {
    stand[k].points = parseInt(stand[k].points) +3;
    stand[k].won = parseInt(stand[k].won) +1;
    }
    
    else if (document.klassement.score11.value > document.klassement.score12.value)
    {
    stand[i].points = parseInt(stand[i].points) +3;
    stand[i].won = parseInt(stand[i].won) +1;
    }
    	
    else if (document.klassement.score11.value == document.klassement.score12.value)
    {
    stand[i].points = parseInt(stand[i].points) +1;
    stand[k].points = parseInt(stand[k].points) +1;
    }
    }
    }	
    }
    }
    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
  • acoder
    Recognized Expert MVP
    • Nov 2006
    • 16032

    #2
    Use the elements array, e.g.
    [code=javascript]document.forms["klassement "].elements["team"+i].value[/code]

    Comment

    • Cainnech
      New Member
      • Nov 2007
      • 132

      #3
      Indeed this solution works perfectly,

      Thank you!

      Comment

      • acoder
        Recognized Expert MVP
        • Nov 2006
        • 16032

        #4
        You're welcome. Glad it worked :)

        Comment

        Working...