Boolean
Boolean

Reputation: 163

Too many elements appearing at once using Hide()/Show() - JQuery


I have been creating a form where there are up to 15 of the same subform:

<div class="form-group" id="PlayerForm1">
    <p>Player: </p>
    <div class="col-md-10">
        <input type="text" id="Player1" value="" />
    </div>
</div>
// more of the same forms but different ids etc.

To make more forms appear I am using a button:

<input type="button" name="AddPlayer" id="AddPlayer" value="Add a Player" />

The JQuery for the button:

$(function () {
  $("#AddPlayer").click(function () {
      for (var i = 0; i < 15; i++) {
          if ($("#PlayerForm" + i).is(":hidden")) {
              $("#PlayerForm" + i).show();
              return;
          }
      }
  });
});

When the button is clicked, two of these forms are shown at once, while I only want one form to be shown every click. Where am I going wrong?
EDIT:
full code:

$(function () {
    $("#AddPlayer").click(function () {
        for (var i = 0; i < 15; i + 1) {
            if ($("#PlayerForm" + i).is(":hidden")) {
                $("#PlayerForm" + i).show();
                return;
            }
        }
    });
    $("#RemovePlayer").click(function () {
        for (var i = 15; i > 0; i - 1) {
            if ($("#PlayerForm" + i).is(":visible")) {
                $("#PlayerForm" + i).hide();
                return;
            }
        }
    });
});

HTML:

<div id="Players">
            <div class="form-group" id="PlayerForm1">
                <p>Player: </p>
                <div class="col-md-10">
                    <input type="text" id="Player1" value="" />
                </div>
            </div>
            <div class="form-group" id="PlayerForm2" style="display:none">
                <p>Player: </p>
                <div class="col-md-10">
                    <input type="text" id="Player2" value=""  />
                </div>
            </div>
            <div class="form-group" id="PlayerForm3" style="display:none">
                <p>Player: </p>
                <div class="col-md-4">
                    <input type="text" id="Player3" value="" />
                </div>
            </div>
            <div class="form-group" id="PlayerForm4" style="display:none">
                <p>Player: </p>
                <div class="col-md-4">
                    <input type="text" id="Player4" value="" />
                </div>
            </div>
            <div class="form-group" id="PlayerForm5" style="display:none">
                <p>Player: </p>
                <div class="col-md-4">
                    <input type="text" id="Player5" value="" />
                </div>
            </div>
            <div class="form-group" id="PlayerForm6" style="display:none">
                <p>Player: </p>
                <div class="col-md-4">
                    <input type="text" id="Player6" value=""  />
                </div>
            </div>
            <div class="form-group" id="PlayerForm7" style="display:none">
                <p>Player: </p>
                <div class="col-md-4">
                    <input type="text" id="Player7" value="" />
                </div>
            </div>
            <div class="form-group" id="PlayerForm8" style="display:none">
                <p>Player: </p>
                <div class="col-md-10">
                    <input type="text" id="Player8" value=""/>
                </div>
            </div>
            <div class="form-group" id="PlayerForm9" style="display:none">
                <p>Player: </p>
                <div class="col-md-10">
                    <input type="text" id="Player9" value=""/>
                </div>
            </div>
            <div class="form-group" id="PlayerForm10" style="display:none">
                <p>Player: </p>
                <div class="col-md-10">
                    <input type="text" id="Player10" value=""/>
                </div>
            </div>
            <div class="form-group" id="PlayerForm11" style="display:none">
                <p>Player: </p>
                <div class="col-md-10">
                    <input type="text" id="Player11" value=""/>
                </div>
            </div>
            <div class="form-group" id="PlayerForm12" style="display:none">
                <p>Player: </p>
                <div class="col-md-10">
                    <input type="text" id="Player12" value=""/>
                </div>
            </div>
            <div class="form-group" id="PlayerForm13" style="display:none">
                <p>Player: </p>
                <div class="col-md-10">
                    <input type="text" id="Player13" value=""/>
                </div>
            </div>
            <div class="form-group" id="PlayerForm14" style="display:none">
                <p>Player: </p>
                <div class="col-md-10">
                    <input type="text" id="Player14" value=""/>
                </div>
            </div>
            <div class="form-group" id="PlayerForm15" style="display:none">
                <p>Player: </p>
                <div class="col-md-10">
                    <input type="text" id="Player15" value=""/>
                </div>
            </div>

Upvotes: 0

Views: 189

Answers (1)

Jamiec
Jamiec

Reputation: 136104

If you just want one item to be shown, then only select one item. You could do this by finding the first hidden form

$('.form-group').hide()

$("#AddPlayer").click(function () {
      $('.form-group:hidden:first').show();
  });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="form-group" id="PlayerForm1">
    <p>Player: </p>
    <div class="col-md-10">
        <input type="text" id="Player1" value="" />
    </div>
</div>
<div class="form-group" id="PlayerForm2">
    <p>Player: </p>
    <div class="col-md-10">
        <input type="text" id="Player2" value="" />
    </div>
</div>
<div class="form-group" id="PlayerForm3">
    <p>Player: </p>
    <div class="col-md-10">
        <input type="text" id="Player2" value="" />
    </div>
</div>
<div class="form-group" id="PlayerForm4">
    <p>Player: </p>
    <div class="col-md-10">
        <input type="text" id="Player4" value="" />
    </div>
</div>

<input type="button" name="AddPlayer" id="AddPlayer" value="Add a Player" />

You can see here ive used your class .form-group as there is no need to target by id.

Upvotes: 3

Related Questions