The first part of my code is working just fine. It has the user type in the number of students in a class, hit submit, and then it returns a response depending on their input. I am trying to also have the next step appear when the user hits submit in the first step. I need the number from the first step (numberOfStudents) to create text fields and a drop down for each student. (firstName, lastName, and studentLevel). EX: User enters 24 and hits submit. Gets response "You will have 6 teams of 4." and the second step appears with 24 instances of the firstName, lastName, and studentLevel entry fields with a submit button at the end. Right now, everything is working except for the loop of studentList to create the 24 (x3) entry fields.
This is also using bootstrap.
I am relatively new to coding, so please explain thoroughly :)
While waiting for a response, I also tried a do-while loop and that will spit out one instance of the 3 entry fields. It doesn't seem to like the i < numberOfStudents.length as a condition.
$("#teamForm").submit(function(event) {
event.preventDefault();
const numberOfStudents = parseInt($("#numberOfStudents").val());
let responseHTML = '<p class="responseText">';
if (numberOfStudents % 4 === 0) {
responseHTML += 'You will have ' + numberOfStudents / 4 + ' teams of 4 in your class.';
} else if (numberOfStudents % 4 === 1) {
responseHTML += 'You will have ' + (numberOfStudents - 1) / 4 + ' teams of 4 in your class and one team of 5.'
} else if (numberOfStudents % 4 === 2) {
responseHTML += 'You will have ' + (numberOfStudents - 6) / 4 + ' teams of 4 in your class and two teams of 3.'
} else {
responseHTML += 'You will have ' + (numberOfStudents - 3) / 4 + ' teams of 4 in your class and one team of 3.'
}
responseHTML += '</p>'
$('#studentNumberResponse').css('display', 'block').html(responseHTML);
$('#numberOfStudents').val('');
}).submit(function(event) {
event.preventDefault();
const numberOfStudents = parseInt($("#numberOfStudents").val());
let responseHTMLSecond = '<div class="card-block"> <h4 class="card-title">Step 2: Enter Your Students</h4> <p class="card-text">Add your students to create each individual team.</p> <form id="studentsForm">';
let studentList = '<div class="form-group"> <input type="text" class="form-control" id="studentFirstName" aria-describedby="studentFirstName" placeholder="First Name"> </div> <div class="form-group"> <input type="text" class="form-control" id="studentLastName" aria-describedby="studentLastName" placeholder="Last Name"> </div> <div class="form-group"> <label for="exampleSelect1">Select Student Level</label> <select class="form-control" id="exampleSelect1"> <option>High</option> <option>Mid-High</option> <option>Mid-Low</option> <option>Low</option> </select> </div>';
for(let i = 0; i < numberOfStudents.length; i +=1) {
responseHTMLSecond += studentList[i];
}
responseHTMLSecond += '<button type="submit" class="btn btn-primary" id="submitStudents">Submit</button> </form> <small class="text-muted">Click the Submit button when you have finished adding all students.</small> </div>';
$('#secondsStep').css('display', 'block');
$('#secondsStep').html(responseHTMLSecond);
});
* {
box-sizing: border-box;
}
#studentNumberResponse,
#secondsStep,
#studentListResponse {
display: none;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<!DOCTYPE HTML>
<html lang="en">
<head>
<meta charset="utf-8">
<title>OnPoint Team Generator</title>
<meta name="description" content="OnPoint Team Generator">
<meta name="author" content="MeganRoberts">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.6/css/bootstrap.min.css" integrity="sha384-rwoIResjU2yc3z8GV/NPeZWAv56rSmLldC3R/AZzGRnGxQQKnKkoFVhFQhNUwEyJ" crossorigin="anonymous">
<script src="http://code.jquery.com/jquery-1.11.0.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.6/js/bootstrap.min.js" integrity="sha384-vBWWzlZJ8ea9aCX4pEW3rVHjgjt7zpkNpZk+02D9phzyeVkE+jo0ieGizqPLForn" crossorigin="anonymous"></script>
<link rel="stylesheet" href="main.css">
</head>
<body>
<div class="card">
<h3 class="card-header" style="text-align: center;">OnPoint Team Generator</h3>
<div class="card-block">
<h4 class="card-title">Step 1: Number of Teams</h4>
<p class="card-text">How many students do you have in your class?</p>
<form id="teamForm">
<div class="form-group">
<input type="text" class="form-control" id="numberOfStudents" aria-describedby="numberStudents" placeholder="Enter Number of Students">
</div>
<button type="submit" class="btn btn-primary" id="submitTeams">Submit</button>
</form>
</div>
</div>
<div class="card">
<div class="card-block" id="studentNumberResponse">
</div>
</div>
<div id="secondsStep" class="card">
</div>
<div class="card">
<div class="card-block" id="studentListResponse">
</div>
</div>
<script src="app.js"></script>
</body>
</html>