user3051963
user3051963

Reputation: 19

Adding form element stops dynamic text box working

I am trying to make dynamic text boxes with Bootstrap for a form I am creating. I have the following code which works:

$(function() {
  $(document).on('click', '.btn-add', function(e) {
    e.preventDefault();

    var controlForm = $('.controls form:first'),
      currentEntry = $(this).parents('.entry:first'),
      newEntry = $(currentEntry.clone()).appendTo(controlForm);

    newEntry.find('input').val('');
    controlForm.find('.entry:not(:last) .btn-add')
      .removeClass('btn-add').addClass('btn-remove')
      .removeClass('btn-success').addClass('btn-danger')
      .html('<span class="glyphicon glyphicon-minus"></span>');
  }).on('click', '.btn-remove', function(e) {
    $(this).parents('.entry:first').remove();

    e.preventDefault();
    return false;
  });
});
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="control-group" id="fields">
  <label class="control-label" for="field1">Nice Multiple Form Fields</label>
  <div class="controls">
    <form role="form" autocomplete="off">
      <div class="entry input-group col-xs-3">
        <input class="form-control" name="fields[]" type="text" placeholder="Type something" />
        <span class="input-group-btn">
                                <button class="btn btn-success btn-add" type="button">
                                    <span class="glyphicon glyphicon-plus"></span>
        </button>

        </span>

      </div>
    </form>
    <br>
    <small>Press <span class="glyphicon glyphicon-plus gs"></span> to add </small>
  </div>
</div>

The problem is when I add a form element around it for some reason it stops working like below:

$(function() {
  $(document).on('click', '.btn-add', function(e) {
    e.preventDefault();

    var controlForm = $('.controls form:first'),
      currentEntry = $(this).parents('.entry:first'),
      newEntry = $(currentEntry.clone()).appendTo(controlForm);

    newEntry.find('input').val('');
    controlForm.find('.entry:not(:last) .btn-add')
      .removeClass('btn-add').addClass('btn-remove')
      .removeClass('btn-success').addClass('btn-danger')
      .html('<span class="glyphicon glyphicon-minus"></span>');
  }).on('click', '.btn-remove', function(e) {
    $(this).parents('.entry:first').remove();

    e.preventDefault();
    return false;
  });
});
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form>
  <div class="control-group" id="fields">
    <label class="control-label" for="field1">Nice Multiple Form Fields</label>
    <div class="controls">
      <form role="form" autocomplete="off">
        <div class="entry input-group col-xs-3">
          <input class="form-control" name="fields[]" type="text" placeholder="Type something" />
          <span class="input-group-btn">
                                <button class="btn btn-success btn-add" type="button">
                                    <span class="glyphicon glyphicon-plus"></span>
          </button>

          </span>

        </div>
      </form>
      <br>
      <small>Press <span class="glyphicon glyphicon-plus gs"></span> to add </small>
    </div>
  </div>
</form>

Any ideas would be appreciated.

Upvotes: 1

Views: 147

Answers (2)

smiller5
smiller5

Reputation: 11

You can't have nested forms

http://www.w3.org/TR/html4/interact/forms.html#h-17.3

A FORM has a mandatory start tag, mandatory end tag and can contain anything in %block or SCRIPT, except other FORMs.

XML DTDs aren't as expressive as SGML DTDs so in XHTML this rule is specified only in the human readable text of the specification:

form must not contain other form elements.

http://www.w3.org/TR/xhtml1/#prohibitions

Upvotes: 1

nisar
nisar

Reputation: 1065

nested form is not possible in html...nested form is not possible...

In a word, no. You can have several forms in a page but they should not be nested. 4.10.3 The form element

Content model:

Flow content, but with no form element descendants.

<div class="control-group" id="fields">
    <label class="control-label" for="field1">Nice Multiple Form Fields</label>
    <div class="controls">
      <form role="form" autocomplete="off">
        <div class="entry input-group col-xs-3">
          <input class="form-control" name="fields[]" type="text" placeholder="Type something" />
          <span class="input-group-btn">
                                <button class="btn btn-success btn-add" type="button">
                                    <span class="glyphicon glyphicon-plus"></span>
          </button>

          </span>

        </div>
      </form>
      <br>
      <small>Press <span class="glyphicon glyphicon-plus gs"></span> to add </small>
    </div>
  </div>


$(function() {
  $(document).on('click', '.btn-add', function(e) {
    e.preventDefault();

    var controlForm = $('.controls form:first'),
      currentEntry = $(this).parents('.entry:first'),
      newEntry = $(currentEntry.clone()).appendTo(controlForm);

    newEntry.find('input').val('');
    controlForm.find('.entry:not(:last) .btn-add')
      .removeClass('btn-add').addClass('btn-remove')
      .removeClass('btn-success').addClass('btn-danger')
      .html('<span class="glyphicon glyphicon-minus"></span>');
  }).on('click', '.btn-remove', function(e) {
    $(this).parents('.entry:first').remove();

    e.preventDefault();
    return false;
  });
});

Upvotes: 1

Related Questions