Skip to content
This repository was archived by the owner on Dec 12, 2021. It is now read-only.

How To: Limit max count of nested fields

jenkek edited this page May 28, 2015 · 4 revisions

This script hides or show 'Add new' link depending on how many (data-limit attribute) nested fields are on a page.

$(function() {

  $(document).on('nested:fieldAdded', function(e) {
    var link = $(e.currentTarget.activeElement);
    if (!link.data('limit')) {
      return;
    }
    if (link.siblings('.fields:visible').length >= link.data('limit')) {
      link.hide();
    }
  });

  $(document).on('nested:fieldRemoved', function(e) {
    var link = $(e.target).siblings('a.add_nested_fields');
    if (!link.data('limit')) {
      return;
    }
    if (link.siblings('.fields:visible').length < link.data('limit')) {
      link.show();
    }
  });

})
Clone this wiki locally