I have a page with a set number of dropdown's on it at the moment (4), these dropdowns are used to select data from a list and submit it. It is used for selecting teams for a tournament, so at the moment I can only create a tournament with 4 teams.
I want to change it so the page displays only one dropdown saying something like Enter a number of teams
and the user selects 4/8/16/32 for example. Based on what number the user selects, the page should then generate this number of dropdowns.
I'm having difficulty doing this, whatever number the user selects, the page then needs to render that amount of dropdowns and name them accordingly i.e #team1, #team2, #team3
.
Any ideas how to do this?
Here are my dropdowns at the moment, using Jade template, sorry if there's any confusion by using Jade but it's easy to understand what it is:
div.newMatch
form.form-horizontal(action='/tournament', id="tournamentForm")
div.row
label.control-label(for="tournamentName") Tournament Name:
div.controls
input#tournamentName.input-mysize(type="text")
div.row
label.control-label(for="team1") Team 1:
div.controls
select#team1(style='width: 160px;')
include teamsDropDown
div.row
label.control-label(for="team2") Team 2:
div.controls
select#team2(style='width: 160px;')
include teamsDropDown
div.row
label.control-label(for="team3") Team 3:
div.controls
select#team3(style='width: 160px;')
include teamsDropDown
div.row
label.control-label(for="team4") Team 4:
div.controls
select#team4(style='width: 160px;')
include teamsDropDown
What I've tried:
div.row
label.control-label(for="tournamentName") Tournament Name:
div.controls
input#tournamentName.input-mysize(type="text")
div.row
label.control-label(for="nTeamSelector") Tournament Size:
div.controls
select#nTeamSelector(style='width: 160px;')
option Please select...
option 2
option 4
option 8
option 16
option 32
div.row.hidden#rowTemplate
label.control-label(for="team") Team:
div.controls
select#team1(style='width: 160px;')
include teamsDropDown
div.row#rowContainer
script(type='text/javascript')
$("#nTeamSelector").change(function(){
var nTeams = $("#nTeamSelector").val(); // Get the value of the team selector that you use
$("#rowContainer").empty() // Delete all existing dropdowns
for (var i=0; i<nTeams; i++){
var newRow = $("#rowTemplate").clone(); // Exact copy of the element generated by Jade
/*
* here you should change all the little fields that
* make the dropdowns different. For example:
*/
newRow.children(".control-label").setText("Team "+(i+1)+":");
$("#rowContainer").append(newRow);
}
});