Skip to content

Commit ad314aa

Browse files
authored
Merge pull request #4 from kloverde/1.2
Merge branch 1.2 to master
2 parents 6442f41 + f74ffce commit ad314aa

File tree

6 files changed

+78
-10
lines changed

6 files changed

+78
-10
lines changed

CHANGELOG.md

+6
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
## Release 1.2 (August 31, 20106)
2+
3+
* Added callbacks for add/remove operations
4+
* Throw an exeption if initialized with a non-existent ID
5+
6+
17
## Release 1.1.2 (August 29, 2016)
28

39
* Changed an occurrence of prop() to attr() - no functional impact, but made for the sake of correctness

README.md

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
AddRemoveTextbox v1.1.2
2-
=======================
1+
AddRemoveTextbox v1.2
2+
=====================
33

44
See LICENSE for this software's licensing terms.
55

@@ -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+
* Configurable callbacks for add and remove operations
1516
* Able to renumber id and name attributes to keep them contiguous (enabled through a configuration option)
1617

1718

@@ -83,6 +84,8 @@ See the included HTML file for more in-depth examples.
8384
| `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) |
8485
| `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 |
8586
| `startingNumber` | This setting is used only when `contiguous` is set to `true`. | null |
87+
| `addCallback` | A callback function to execute when a field is added. See the demo for more information. | null |
88+
| `removeCallback` | A callback function to execute when a field is removed. See the demo for more information. | null |
8689

8790

8891
## Thanks

demo/css/demo.css

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* AddRemoveTextbox v1.1.2
2+
* AddRemoveTextbox v1.2
33
* https://www.github.com/kloverde/jquery-AddRemoveTextbox
44
*
55
* Donations: https://paypal.me/KurtisLoVerde/5

demo/demo.html

+34-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<!DOCTYPE html>
22

33
<!--
4-
AddRemoveTextbox v1.1.2
4+
AddRemoveTextbox v1.2
55
https://www.github.com/kloverde/jquery-AddRemoveTextbox
66
77
Donations: https://paypal.me/KurtisLoVerde/5
@@ -55,6 +55,14 @@
5555
<script type="text/javascript" src="../jquery.addremovetextbox.js"></script>
5656

5757
<script type="text/javascript">
58+
function callbackDemoAdd( id, name ) {
59+
alert( "Added field with id = " + id + ", name = " + name );
60+
}
61+
62+
function callbackDemoRemove( id, name, val ) {
63+
alert( "Field '" + id + "' was removed. Its value was '" + val + "'" );
64+
}
65+
5866
$( document ).ready( function() {
5967
// Example 1
6068

@@ -109,6 +117,15 @@
109117
removeButtonClass : "addRemoveButton imgRemove",
110118
maxFields : 3
111119
} );
120+
121+
// Example 7
122+
123+
$( "#callbackDemo1" ).addRemoveTextbox( {
124+
addButtonClass : "addRemoveButton imgAdd",
125+
removeButtonClass : "addRemoveButton imgRemove",
126+
addCallback : callbackDemoAdd,
127+
removeCallback : callbackDemoRemove
128+
} );
112129
} );
113130
</script>
114131
</head>
@@ -218,6 +235,22 @@ <h3>Example 6: Setting a limit on how many fields can be generated</h3>
218235
</div>
219236
</div>
220237

238+
<h3>Example 7: Callbacks</h3>
239+
<p>
240+
This example defines a callback that gets triggered when a field is added.
241+
The callback then adds a blur callback to watch it for changes. Try it:
242+
add a field, type a value into it, then click outside the field.
243+
</p>
244+
<p>
245+
This example also defines a callback that gets triggered when a field is
246+
removed. Remove a field and see what happens.
247+
</p>
248+
<div id="example7">
249+
<div class="txtBoxRow">
250+
<input type="text" id="callbackDemo1"/>
251+
</div>
252+
</div>
253+
221254
<h2>Donations</h2>
222255
<p>
223256
I hope you found the code behind these examples helpful. Please consider donating to this project to show your

jquery.addremovetextbox.js

+30-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* AddRemoveTextbox v1.1.2
2+
* AddRemoveTextbox v1.2
33
* https://www.github.com/kloverde/jquery-AddRemoveTextbox
44
*
55
* Donations: https://paypal.me/KurtisLoVerde/5
@@ -62,9 +62,24 @@
6262
contiguous : false,
6363

6464
// This setting is used only when 'contiguous' is set to true.
65-
startingNumber : 0
65+
startingNumber : 0,
66+
67+
// A callback function for add operations. Your callback must accept two string arguments:
68+
// arg1: the id attribute of the added row
69+
// arg2: the name attribute of the added row
70+
addCallback : null,
71+
72+
// A callback function for remove operations. Your callback must accept three string arguments:
73+
// arg1: the id attribute of the removed row
74+
// arg2: the name attribute of the removed row
75+
// arg3: the value of the removed row
76+
removeCallback : null
6677
}, options );
6778

79+
if( this.length === 0 || this == null ) {
80+
throwException( "No matching field(s) found" );
81+
}
82+
6883
var ID_PREFIX = this.attr( "id" ).replace( /\[?[0-9]*\]?$/, "" );
6984
var IS_NUMBERED_NOTATION = this.attr( "id" ).match( /[0-9]+$/ ) != null;
7085
var IS_ARRAY_NOTATION = this.attr( "id" ).match( /\[[0-9]\]$/ ) != null;
@@ -213,6 +228,10 @@
213228

214229
newRow.insertAfter( lastRow );
215230

231+
if( typeof settings.addCallback === "function" ) {
232+
settings.addCallback( nextIdResults.nextId, nextIdResults.nextId );
233+
}
234+
216235
return nextIdResults.nextId;
217236
}
218237

@@ -239,7 +258,10 @@
239258
function removeRow( button ) {
240259
var rowToRemove = button.parent();
241260
var prevRow = rowToRemove.prev();
242-
var inputId = rowToRemove.children( "input" ).attr( "id" );
261+
var inputToRemove = rowToRemove.children( "input" );
262+
var inputToRemoveVal = inputToRemove.val();
263+
var inputId = inputToRemove.attr( "id" );
264+
var inputName = inputToRemove.attr( "name" );
243265

244266
// Don't remove the row if it's the only/last one in the group.
245267

@@ -264,7 +286,11 @@
264286
rowToRemove.remove();
265287
rebuild();
266288
} else {
267-
$( escape("#" + inputId) ).val( "" );
289+
inputToRemove.val( "" );
290+
}
291+
292+
if( typeof settings.removeCallback === "function" ) {
293+
settings.removeCallback( inputId, inputName, inputToRemoveVal );
268294
}
269295
}
270296

jquery.addremovetextbox.min.js

+2-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)