1

I'm trying to figure out how to add text boxes after hitting the "submit" button. I've tried everything I know, but I can't seem to get an outcome. I also want to make the max number of text boxes 25.

Here's some of the HTML:

 <div class="title">
    <h1>Calculated Test Scores</h1>
 </div>
    <h5># of Students: <input type="text"></h5>
    <div class="container">
    <button type="button" class="submit">Submit</button>
    </div>
    <h5>Scores: <input type="text"></h5>
    <div class="container">
    <button type="button" class="calculate">Calculate</button>
    </div>
    <h6>The average is:</h6>

And this is the JS:

$(function() {

    $(".submit").click(function() {
        for(var i=0; i < 26; i++)
        $(".submit").append(arr[i])
    });

    $(".calculate").click(function() {
        for(var i=0; i < arr.length; i++)
        sum=sum + arr[i];
        Average = sum/arr.length;
    });

});
6
  • Can you post arr[] too? Commented Mar 15, 2018 at 19:12
  • var arr = [i < 26]; Commented Mar 15, 2018 at 19:17
  • One last thing, I'm trying to sum up the numbers that the user enters. In other words, calculate the average of all the numbers outputted. Commented Mar 15, 2018 at 19:18
  • 2
    You should really read about DOM structure and how to manipulate it. Looks like you are not familiar with core concepts. Commented Mar 15, 2018 at 19:20
  • Can you provide full js code? Commented Mar 15, 2018 at 19:22

2 Answers 2

1

I am not sure what is arr but I simply added several inputs i.e. 25 into that. Here is

jsfiddle

var arr = ['<input type="text" name="name" />', '<input type="text" name="name" />', '<input type="text" name="name" />', '<input type="text" name="name" />', '<input type="text" name="name" />', '<input type="text" name="name" />', '<input type="text" name="name" />', '<input type="text" name="name" />', '<input type="text" name="name" />', '<input type="text" name="name" />','<input type="text" name="name" />','<input type="text" name="name" />','<input type="text" name="name" />','<input type="text" name="name" />','<input type="text" name="name" />','<input type="text" name="name" />','<input type="text" name="name" />','<input type="text" name="name" />','<input type="text" name="name" />','<input type="text" name="name" />','<input type="text" name="name" />','<input type="text" name="name" />','<input type="text" name="name" />','<input type="text" name="name" />','<input type="text" name="name" />'];
$(".submit").click(function() {
    for(var i=0; i < 26; i++) {
      $(".inputs").append(arr[i]);
    }
});

$(".calculate").click(function() {
    for(var i=0; i < arr.length; i++)
    sum=sum + arr[i];
    Average = sum/arr.length;
});

Let me know if this is something you are looking for.

Sign up to request clarification or add additional context in comments.

3 Comments

This is kind of what I'm looking for, but for some reason there are two Calculate buttons. It works in the jsfiddle, but it doesn't work in the program I'm using.
Just realized that the Calculate code was doubled in my program.
If my answer helped you find solution than don't forget to accept my answer.
0

Please see the working prototype (markup slightly modified):

$(".submit").click(function() {
  var newContent = "";
  for (var i = 0; i < $("#scount").val(); i++)
    newContent += "<input type='text'/>";
  $(".dynamic_inputs").html(newContent);
});

$(".calculate").click(function() {
  var sum = 0;
  $(".dynamic_inputs input").each(function() {
    sum = sum + parseInt($(this).val());
  });
  var avg = sum / $(".dynamic_inputs input").length;
  $("h6").text("The average is: " + avg);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<div class="title">
  <h1>Calculated Test Scores</h1>
</div>
<h5># of Students: <input id="scount" type="text"></h5>
<div class="container">
  <button type="button" class="submit">Submit</button>
</div>
<h5>Scores: </h5>
<div class="dynamic_inputs"></div>
<hr/>
<div class="container">
  <button type="button" class="calculate">Calculate</button>
</div>
<h6>The average is:</h6>

1 Comment

Akhil, the prototype isn't working. I enter a # of students and hit submit, but no other text boxes appear to show up under scores.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.