Code

Add/Remove Form Input Buttons

Add or remove form input fields with buttons on your Webflow site. This allows your users to add or remove form fields based on buttons on your Webflow site. A great way of adding dynamic form elements to your Webflow site.

Add/Remove Form Input Buttons preview

Add or remove form input fields with buttons on your Webflow site. This allows your users to add or remove form fields based on buttons on your Webflow site. A great way of adding dynamic form elements to your Webflow site.

How to add it

  1. Review the HTML to understand class names and structure or as an embed
  2. Add the Javascript to the closing body section

The code

Javascript
                      <!-- Add to closing body -->
<script>
$(document).ready(function() {
  var buttonAdd = $("#add-button");
  var buttonRemove = $("#remove-button");
  var className = ".dynamic-field";
  var count = 0;
  var field = "";
  var maxFields = 5;

  function totalFields() {
    return $(className).length;
  }

  function addNewField() {
    count = totalFields() + 1;
    field = $("#dynamic-field-1").clone();
    field.attr("id", "dynamic-field-" + count);
    field.children("label").attr("for", "additional-field" + count);
    field.children("input").attr("id", "additional-field" + count);
		field.children("input").attr("data-name", "additional-field" + count);
		field.children("input").attr("name", "additional-field" + count);
    field.find("input").val("");
    $(className + ":last").after($(field));
  }

  function removeLastField() {
    if (totalFields() > 1) {
      $(className + ":last").remove();
    }
  }

  function enableButtonRemove() {
    if (totalFields() === 2) {
      buttonRemove.removeAttr("disabled");
      buttonRemove.addClass("shadow-sm");
    }
  }

  function disableButtonRemove() {
    if (totalFields() === 1) {
      buttonRemove.attr("disabled", "disabled");
      buttonRemove.removeClass("shadow-sm");
    }
  }

  function disableButtonAdd() {
    if (totalFields() === maxFields) {
      buttonAdd.attr("disabled", "disabled");
      buttonAdd.removeClass("shadow-sm");
    }
  }

  function enableButtonAdd() {
    if (totalFields() === (maxFields - 1)) {
      buttonAdd.removeAttr("disabled");
      buttonAdd.addClass("shadow-sm");
    }
  }

  buttonAdd.click(function() {
    addNewField();
    enableButtonRemove();
    disableButtonAdd();
  });

  buttonRemove.click(function() {
    removeLastField();
    disableButtonRemove();
    enableButtonAdd();
  });
});
</script>

                    
HTML
                      <!-- Add as an embed -->
<form action="" method="post">
    <div id="dynamic-field-1" class="form-group dynamic-field">
      <label for="field" class="font-weight-bold">Field 1</label>
      <input type="text" id="field" class="form-control" name="field[]" />
    </div>
    <div class="clearfix mt-4">
      <button type="button" id="add-button" class="btn btn-secondary float-left text-uppercase shadow-sm"><i class="fas fa-plus fa-fw"></i> Add</button>
      <button type="button" id="remove-button" class="btn btn-secondary float-left text-uppercase ml-1" disabled="disabled"><i class="fas fa-minus fa-fw"></i> Remove</button>
      <button type="submit" class="btn btn-primary float-right text-uppercase shadow-sm">Submit</button>
    </div>
</form>

                    

Browse similar code snippets

Categories

Language

More from Corey Moen

Profile →

Related Webflow questions

All answers →

Similar code snippets

All code snippets →