Skip to content

Commit b591d2e

Browse files
author
Kurtis LoVerde
committed
Added renumbering option; embed license in source files
1 parent 7391d8b commit b591d2e

File tree

7 files changed

+195
-28
lines changed

7 files changed

+195
-28
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/.project

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
## Release 1.1 (August 28, 2016)
2+
3+
* Added an option to renumber id and name attributes to ensure they remain contiguous
4+
5+
16
## Release 1.0 (August 28, 2016)
27

38
* First release

README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
AddRemoveTextbox v1.0
1+
AddRemoveTextbox v1.1
22
=====================
33

44
See LICENSE for this software's licensing terms.
@@ -12,6 +12,7 @@ AddRemoveTextbox is a jQuery plugin which provides for dynamic creation and remo
1212
* Configurable CSS classes for the 'add' and 'remove' buttons
1313
* Configurable tooltips for the 'add' and 'remove' buttons
1414
* Configurable limit on the number of input fields
15+
* Able to renumber id and name attributes to keep them contiguous (enabled through a configuration option)
1516

1617

1718
## jQuery Compatibility
@@ -80,6 +81,8 @@ See the included HTML file for more in-depth examples.
8081
| `removeButtonClass` | A CSS class to style the 'Remove' button. The generated HTML will be a <span>, so it is assumed that you will use CSS to define background image. | removeButton |
8182
| `removeButtonTooltip` | Hover text for the 'Remove' button | null |
8283
| `maxFields` | An optional limit on the number of fields which may exist under the applicable ID prefix. If a value is specified, it must be an integer greater than 1. | null (no limit) |
84+
| `contiguous` | If `true`, renumber the `id` and `name` attributes upon initialization and when a row is removed. Renumbering is based on DOM order, not `id` or `name` values, and starts on the value specified by `startingNumber`. This setting is disabled by default because it can break applications. | false |
85+
| `startingNumber` | This setting is used only when `contiguous` is set to `true`. | null |
8386

8487

8588
## Thanks

demo/css/demo.css

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,34 @@
11
/*
2-
* AddRemoveTextbox v1.0
2+
* AddRemoveTextbox v1.1
33
* https://www.github.com/kloverde/jquery-AddRemoveTextbox
44
*
5-
* This software is licensed under the 3-clause BSD license.
5+
* Donations: https://paypal.me/KurtisLoVerde/5
66
*
7-
* Copyright (c) 2016 Kurtis LoVerde
8-
* All rights reserved
7+
* Copyright (c) 2016, Kurtis LoVerde
8+
* All rights reserved.
99
*
10-
* Donations: https://paypal.me/KurtisLoVerde/5
10+
* Redistribution and use in source and binary forms, with or without
11+
* modification, are permitted provided that the following conditions are met:
12+
*
13+
* 1. Redistributions of source code must retain the above copyright
14+
* notice, this list of conditions and the following disclaimer.
15+
* 2. Redistributions in binary form must reproduce the above copyright
16+
* notice, this list of conditions and the following disclaimer in the
17+
* documentation and/or other materials provided with the distribution.
18+
* 3. Neither the name of the copyright holder nor the names of its
19+
* contributors may be used to endorse or promote products derived from
20+
* this software without specific prior written permission.
21+
*
22+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
23+
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
24+
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
25+
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
26+
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27+
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
28+
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29+
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30+
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31+
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
1132
*/
1233

1334
.txtBoxRow {

demo/demo.html

Lines changed: 61 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,36 @@
11
<!DOCTYPE html>
22

33
<!--
4-
AddRemoveTextbox v1.0
4+
AddRemoveTextbox v1.1
55
https://www.github.com/kloverde/jquery-AddRemoveTextbox
6-
7-
Copyright (c) 2016 Kurtis LoVerde
8-
All rights reserved
9-
6+
107
Donations: https://paypal.me/KurtisLoVerde/5
8+
9+
Copyright (c) 2016, Kurtis LoVerde
10+
All rights reserved.
11+
12+
Redistribution and use in source and binary forms, with or without
13+
modification, are permitted provided that the following conditions are met:
14+
15+
1. Redistributions of source code must retain the above copyright
16+
notice, this list of conditions and the following disclaimer.
17+
2. Redistributions in binary form must reproduce the above copyright
18+
notice, this list of conditions and the following disclaimer in the
19+
documentation and/or other materials provided with the distribution.
20+
3. Neither the name of the copyright holder nor the names of its
21+
contributors may be used to endorse or promote products derived from
22+
this software without specific prior written permission.
23+
24+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
25+
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
26+
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
27+
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
28+
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29+
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
30+
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
31+
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
32+
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
33+
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
1134
-->
1235

1336
<html lang="en-US">
@@ -62,14 +85,15 @@
6285

6386
// Example 4
6487

65-
$( "#stuff1" ).addRemoveTextbox( {
88+
$( "#renumber4" ).addRemoveTextbox( {
6689
addButtonClass : "addRemoveButton imgAdd",
6790
addButtonTooltip : "Add",
6891
removeButtonClass : "addRemoveButton imgRemove",
69-
removeButtonTooltip : "Remove"
92+
removeButtonTooltip : "Remove",
93+
contiguous : true
7094
} );
7195

72-
// Example 5
96+
// Example 6
7397

7498
$( "#maxFields1" ).addRemoveTextbox( {
7599
addButtonClass : "addRemoveButton imgAdd",
@@ -146,13 +170,40 @@ <h3>Example 4: Pre-existing fields, IDs out of order and with gaps</h3>
146170
</div>
147171
</div>
148172

149-
<h3>Example 5: Setting a limit on how many fields can be generated</h3>
173+
<h3>Example 5 - Renumbering: Pre-existing fields, IDs out of order and with gaps</h3>
174+
<p>
175+
These fields' IDs and names remain contiguous. Compare what the code says to what
176+
your favorite DOM inspector says. This functionality is disabled by default
177+
because it can break applications. Be sure that your application can handle
178+
changing IDs and names before using this.
179+
</p>
180+
181+
<div id="example5">
182+
<div class="txtBoxRow">
183+
<input type="text" id="renumber3" name="renumber3" value="Hello"/>
184+
</div>
185+
186+
<div class="txtBoxRow">
187+
<input type="text" id="renumber8" name="renumber8" value="Konichiwa"/>
188+
</div>
189+
190+
<div class="txtBoxRow">
191+
<input type="text" id="renumber6" name="renumber6" value="Bonjour"/>
192+
</div>
193+
194+
<div class="txtBoxRow">
195+
<input type="text" id="renumber4" name="renumber4" value="Hola"/>
196+
</div>
197+
</div>
198+
199+
200+
<h3>Example 6: Setting a limit on how many fields can be generated</h3>
150201
<p>
151202
This is set to allow 3 fields. You won't be able to add more after that,
152203
but if you delete one of them, you'll be able to add one again.
153204
</p>
154205

155-
<div id="example5">
206+
<div id="example6">
156207
<div class="txtBoxRow">
157208
<input type="text" id="maxFields1" name="maxFields1"/>
158209
</div>

jquery.addremovetextbox.js

Lines changed: 71 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,37 @@
11
/*
2-
* AddRemoveTextbox v1.0
2+
* AddRemoveTextbox v1.1
33
* https://www.github.com/kloverde/jquery-AddRemoveTextbox
44
*
5-
* This software is licensed under the 3-clause BSD license.
5+
* Donations: https://paypal.me/KurtisLoVerde/5
66
*
7-
* Copyright (c) 2016 Kurtis LoVerde
8-
* All rights reserved
7+
* Copyright (c) 2016, Kurtis LoVerde
8+
* All rights reserved.
99
*
10-
* Donations: https://paypal.me/KurtisLoVerde/5
10+
* Redistribution and use in source and binary forms, with or without
11+
* modification, are permitted provided that the following conditions are met:
12+
*
13+
* 1. Redistributions of source code must retain the above copyright
14+
* notice, this list of conditions and the following disclaimer.
15+
* 2. Redistributions in binary form must reproduce the above copyright
16+
* notice, this list of conditions and the following disclaimer in the
17+
* documentation and/or other materials provided with the distribution.
18+
* 3. Neither the name of the copyright holder nor the names of its
19+
* contributors may be used to endorse or promote products derived from
20+
* this software without specific prior written permission.
21+
*
22+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
23+
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
24+
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
25+
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
26+
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27+
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
28+
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29+
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30+
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31+
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
1132
*/
1233

34+
1335
(function( $ ) {
1436
"use strict";
1537

@@ -32,7 +54,15 @@
3254

3355
// A limit on the number of fields which may exist under the applicable ID prefix. Default
3456
// is null (no limit). If a value is specified, it must be an integer greater than 1.
35-
maxFields : null
57+
maxFields : null,
58+
59+
// If true, renumber the id and name attributes upon initialization and when a row is removed.
60+
// Renumbering is based on DOM order, not `id` or `name` values, and starts on the value
61+
// specified by `startingNumber`. This setting is disabled by default because it can break applications.
62+
contiguous : false,
63+
64+
// This setting is used only when 'contiguous' is set to true.
65+
startingNumber : 0
3666
}, options );
3767

3868
var ID_PREFIX = this.attr( "id" ).replace( /\[?[0-9]*\]?$/, "" );
@@ -59,6 +89,22 @@
5989
}
6090
}
6191

92+
if( settings.contiguous === true ) {
93+
if( settings.startingNumber != null ) {
94+
var errMsg = "startingNumber (" + settings.startingNumber + ") must be an integer greater than or equal to 0";
95+
96+
if( isNaN(settings.startingNumber) ) {
97+
throwException( errMsg );
98+
} else {
99+
settings.startingNumber = parseInt( settings.startingNumber );
100+
101+
if( settings.startingNumber < 0 || settings.startingNumber % 1 !== 0 ) {
102+
throwException( errMsg );
103+
}
104+
}
105+
}
106+
}
107+
62108
init( this );
63109

64110
function throwException( msg ) {
@@ -83,6 +129,8 @@
83129
makeAddButton().appendTo( parentRow );
84130
}
85131
} );
132+
133+
rebuild();
86134
}
87135

88136
function makeButton( className, title ) {
@@ -214,11 +262,28 @@
214262
}
215263

216264
rowToRemove.remove();
265+
rebuild();
217266
} else {
218267
$( escape("#" + inputId) ).val( "" );
219268
}
220269
}
221270

271+
function rebuild() {
272+
if( settings.contiguous === true ) {
273+
var currNum = settings.startingNumber;
274+
275+
$( "input[id^=" + ID_PREFIX + "]" ).each( function() {
276+
var field = $(this);
277+
var newId = IS_ARRAY_NOTATION ? ID_PREFIX + "[" + currNum + "]"
278+
: ID_PREFIX + currNum;
279+
field.attr( "id", newId );
280+
field.attr( "name", newId );
281+
282+
currNum++;
283+
} );
284+
}
285+
}
286+
222287
function escape( selector ) {
223288
return selector.replace( /(\[|\])/g, "\\$1" );
224289
}

0 commit comments

Comments
 (0)