Giter Club home page Giter Club logo

dynform's Introduction

dynform

A simple jQuery plugin to dynamically replicate form fields.

dynform will replicate a set of elements usually in a form based on simple rules. The most common usecase would be to replicate an input field when the user starts typing to allow for multiple inputs. For example to allow a user to enter multiple phone numbers. First only one input would be visible, but as soon as the user entered the first number, a second input apears.

The plugin can be used multiple times per form and page.

<form>
	<input id="notDynamic">
	<div id="dynamic_components">
		<input id="willReplicate">
	</div>
</form>

<script>
$('#dynamic_components').dynform();
</script>

Example

See [example.html].

Options

dynform can be initialised with various options:

  • tpl - String

    A html template to replicate. If none is given, the content of the dynform element is used.

  • trigger - String

    A selector to use for identifying replication triggers (inputs) inside the replication template. Defaults to .dynform-trigger.

  • max - Number

    Maximum number of elements to replicate.

  • events - Object

    The object has two keys: add and rm that both have an array of Strings as value. Each string should be a valid event that can be passed to on. These events will be bound to the triggers and invoke the addition and removal of replicated elements.

    Default:

     {
     	events: {
     		add: ['change', 'keypress'],
     		rm: ['change']
     	}
     }
  • callback - Object

    Can have three keys with callback functions as values: add, rm and renumber.

  • add - Function

    add: function(new_element) { return true; }

    The add callback is invoked every time a new element is about to be added, but before the element is actually appended. The callback will receive the new element as an argument. It may veto the addition by returning false.

  • rm - Function

    rm: function(removed_elements){ return removed_elements; }

    The rm callback is invoked when replicated elements are about to get removed from the dynform. It will receive all elements the will get removed as an array. It may veto the removal by removing elements from and returning the truncated array.

  • renumber - Function

    renumber: function(no, element){}

    The renumber callback is called once for every element when the element ids are renumbered. It will receive the number of the element and the element itself as arguments. This callback is useful for also renumbering labels and other custom text inside the form.

  • keep - Function

     keep: function(triggers, element) { return boolean; }

    The keep function is used to decide if an element should be removed from the dynform or not. By default the plugin will check every trigger of the element for its value and if they all are empty the element is removed. For more complex behavior (e.g. checking inputs that are not triggers themselves) you can provide your own logic here.

Notes

  • dynform element will be emptied
  • dynform elements should be wrapped in a div

dynform's People

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.