Now on npm. Using vanilla JavaScript (ES6) or jQuery to duplicate a section of a form, maintaining accessibility (a11y).
Plain JavaScript version now available on npm! (Currently in alpha.)
Using jQuery or plain JavaScript (ES6), you can now duplicate a section of a form (as well as destroy the last cloned section), the whole form, or just one or two inputs. This also allows the user to add an empty form section with unique, iterative ID
, for
, name
, and label
to maintain accessibility (a11y) and work with the database.
To help you get a jumpstart on this, the .js files are well-commented.
A more detailed overview can be found here. I will be working on porting it over to this page soon. (Still working on this todo.)
The jQuery version has been in development since June 2013, and is more stable, at the moment.
The ES6 version was a direct port over from the jQuery version. While it works in the same way the jQuery one does, there are some notable todos and refactoring that needs to be done before it can be used in production environments.
This cloning script is built to:
for
, id
and name
attributes (ID2_, ID3_, ID4_) so as to be unique pairsThis is not a drop-in-and-it-works solution. You can see in the .js files that we depend on querying for classnames to update the for
, id
and name
attributes of inputs, among other things.
clonedInput
.for
, id
and name
attributes; removing sections…)for
, id
and name
attributes (ID2_, ID3_, ID4_) of inputsThe ES6 demo can be viewed here (download repo and view).
The Bootstrap 3.4 demo for duplicating a whole section of a form can be found here.
The Bootstrap 3.4 demo supporting duplicating multiple elements independently can be found here.
I found a pen that uses version 0.9.4.1 to create a simple form for a calculator. View it here.
You can best reach me by using my contact page.
Or on Twitter: @tristandenyer
The MIT License (MIT)