bennever
09-03-2009, 10:11 AM
Hi, I have developed a website for my snooker league and have used the 2 drop down list technique to select team players from a team list. This all works well although is somewhat wasteful of code!
I have tried to reduce the coding by creating the drop down list of players by using a for loop but the result is that the player's name is recognised by js as a string and not as the defined variable.
The link to the web page is here (http://www.ennever.com/snooker/submitresults5.shtml) and the js includes this code:
=========================================
if(document.drop_list.Hometeam.value == TeamA){
var i=1;
for (i=1;i<=TeamAPlayers;i++)
{
addOption(document.drop_list.PlayerHome1,['PlayerA'+i],['PlayerA'+i]);
}
addOption(document.drop_list.PlayerHome1,Bye, Bye);
}
===========================================
This is attempting to reduce the working code from this, which needs to be hardcoded every time there is a change in the number of players in a team:
===========================================
if(document.drop_list.Hometeam.value == TeamA){
addOption(document.drop_list.PlayerHome1,PlayerA1, PlayerA1);
addOption(document.drop_list.PlayerHome1,PlayerA2, PlayerA2);
addOption(document.drop_list.PlayerHome1,PlayerA3, PlayerA3);
addOption(document.drop_list.PlayerHome1,PlayerA4, PlayerA4);
addOption(document.drop_list.PlayerHome1,PlayerA5, PlayerA5);
addOption(document.drop_list.PlayerHome1,PlayerA6, PlayerA6);
addOption(document.drop_list.PlayerHome1,PlayerA7, PlayerA7);
addOption(document.drop_list.PlayerHome1,PlayerA8, PlayerA8);
addOption(document.drop_list.PlayerHome1,PlayerA9, PlayerA9);
addOption(document.drop_list.PlayerHome1,PlayerA10 , PlayerA10);
addOption(document.drop_list.PlayerHome1,Bye, Bye);
}
===========================================
In both cases the following vars are pre-defined:
var TeamAPlayers = 6; ie no of players to loop through & create list from
var TeamA = "A"; sample team name
var PlayerA1 = "MeA1"; etc etc to A10 sample team players
var Bye = "Bye"; always needed!
Can anyone offer me a solution where the "addOption" line references the defined variable please???
Many thanks for your help
Barry
I have tried to reduce the coding by creating the drop down list of players by using a for loop but the result is that the player's name is recognised by js as a string and not as the defined variable.
The link to the web page is here (http://www.ennever.com/snooker/submitresults5.shtml) and the js includes this code:
=========================================
if(document.drop_list.Hometeam.value == TeamA){
var i=1;
for (i=1;i<=TeamAPlayers;i++)
{
addOption(document.drop_list.PlayerHome1,['PlayerA'+i],['PlayerA'+i]);
}
addOption(document.drop_list.PlayerHome1,Bye, Bye);
}
===========================================
This is attempting to reduce the working code from this, which needs to be hardcoded every time there is a change in the number of players in a team:
===========================================
if(document.drop_list.Hometeam.value == TeamA){
addOption(document.drop_list.PlayerHome1,PlayerA1, PlayerA1);
addOption(document.drop_list.PlayerHome1,PlayerA2, PlayerA2);
addOption(document.drop_list.PlayerHome1,PlayerA3, PlayerA3);
addOption(document.drop_list.PlayerHome1,PlayerA4, PlayerA4);
addOption(document.drop_list.PlayerHome1,PlayerA5, PlayerA5);
addOption(document.drop_list.PlayerHome1,PlayerA6, PlayerA6);
addOption(document.drop_list.PlayerHome1,PlayerA7, PlayerA7);
addOption(document.drop_list.PlayerHome1,PlayerA8, PlayerA8);
addOption(document.drop_list.PlayerHome1,PlayerA9, PlayerA9);
addOption(document.drop_list.PlayerHome1,PlayerA10 , PlayerA10);
addOption(document.drop_list.PlayerHome1,Bye, Bye);
}
===========================================
In both cases the following vars are pre-defined:
var TeamAPlayers = 6; ie no of players to loop through & create list from
var TeamA = "A"; sample team name
var PlayerA1 = "MeA1"; etc etc to A10 sample team players
var Bye = "Bye"; always needed!
Can anyone offer me a solution where the "addOption" line references the defined variable please???
Many thanks for your help
Barry