Reputation: 41
I would like to find a more serious code for the one below. I am looping through a number of variables (player1, TSP1, player2, TSP2,...). Here is the ugly code, that works fine:
if (TSP1 < player1) {
$("#errornotice").html("Minimum TSP player 1 " is " + player1);
player1-list.focus();
} else if (TSP2 < player2) {
$("#errornotice").html("Minimum TSP player 2 " is " + player2);
player2-list.focus();
} else if (TSP3 < player3) {
$("#errornotice").html("Minimum TSP player 3 " is " + player3);
player3-list.focus();
} else if (TSP4 < player4) {
$("#errornotice").html("Minimum TSP player 4 " is " + player4);
player4-list.focus();
}
<b><span style="color:red" id ="errornotice"></span></b><br>
How can I get it to do the same less ugly in a loop ? Something like this:
for (var a = 1; a < 4; a++){
if (TSP+a < speler+a) {
$("#errornotice").html("Minimum TSP player" +a+ " is " + player+a);
Thuisspeler1-list+a.focus();
}
}
Obviously, the syntaxis is completely wrong, but that's just to give you an idea of what I would like to achieve. I am really stuck here, so all help is greatly appreciated.
Upvotes: 0
Views: 40
Reputation: 29
Not sure to understand the purpose of the code snippet, so sorry if this look obvious to you, but:
Upvotes: 1
Reputation: 1062
I recommend reading this, How to reference a variable dynamically in javascript .
Upvotes: 0