1
// ===================================================================
2
// Author: Matt Kruse <matt@mattkruse.com>
3
// WWW: http://www.mattkruse.com/
5
// NOTICE: You may use this code for any purpose, commercial or
6
// private, without any further permission from the author. You may
7
// remove this notice from your final code if you wish, however it is
8
// appreciated by the author if at least my web site address is kept.
10
// You may *NOT* re-distribute this code in any way except through its
11
// use. That means, you can include it in your product, or your web
12
// site, or any other form where the code is actually being used. You
13
// may not put the plain javascript up on your site for download or
14
// include it in your javascript libraries for download.
15
// If you wish to share this code with others, please just point them
16
// to the URL instead.
17
// Please DO NOT link directly to my .js files from your site. Copy
18
// the files to your server and use them there. Thank you.
19
// ===================================================================
22
// ------------------------------------------------------------------
23
// June 12, 2003: Modified up and down functions to support more than
24
// one selected option
26
DESCRIPTION: These are general functions to deal with and manipulate
27
select boxes. Also see the OptionTransfer library to more easily
28
handle transferring options between two lists
30
COMPATABILITY: These are fairly basic functions - they should work on
31
all browsers that support Javascript.
35
// -------------------------------------------------------------------
37
// Utility function to determine if a select object has an options array
38
// -------------------------------------------------------------------
39
function hasOptions(obj) {
40
if (obj!=null && obj.options!=null) { return true; }
44
// -------------------------------------------------------------------
45
// selectUnselectMatchingOptions(select_object,regex,select/unselect,true/false)
46
// This is a general function used by the select functions below, to
47
// avoid code duplication
48
// -------------------------------------------------------------------
49
function selectUnselectMatchingOptions(obj,regex,which,only) {
51
if (which == "select") {
55
else if (which == "unselect") {
62
var re = new RegExp(regex);
63
if (!hasOptions(obj)) { return; }
64
for (var i=0; i<obj.options.length; i++) {
65
if (re.test(obj.options[i].text)) {
66
obj.options[i].selected = selected1;
70
obj.options[i].selected = selected2;
77
// -------------------------------------------------------------------
78
// selectMatchingOptions(select_object,regex)
79
// This function selects all options that match the regular expression
80
// passed in. Currently-selected options will not be changed.
81
// -------------------------------------------------------------------
82
function selectMatchingOptions(obj,regex) {
83
selectUnselectMatchingOptions(obj,regex,"select",false);
85
// -------------------------------------------------------------------
86
// selectOnlyMatchingOptions(select_object,regex)
87
// This function selects all options that match the regular expression
88
// passed in. Selected options that don't match will be un-selected.
89
// -------------------------------------------------------------------
90
function selectOnlyMatchingOptions(obj,regex) {
91
selectUnselectMatchingOptions(obj,regex,"select",true);
93
// -------------------------------------------------------------------
94
// unSelectMatchingOptions(select_object,regex)
95
// This function Unselects all options that match the regular expression
97
// -------------------------------------------------------------------
98
function unSelectMatchingOptions(obj,regex) {
99
selectUnselectMatchingOptions(obj,regex,"unselect",false);
102
// -------------------------------------------------------------------
103
// sortSelect(select_object)
104
// Pass this function a SELECT object and the options will be sorted
105
// by their text (display) values
106
// -------------------------------------------------------------------
107
function sortSelect(obj) {
109
if (!hasOptions(obj)) { return; }
110
for (var i=0; i<obj.options.length; i++) {
111
o[o.length] = new Option( obj.options[i].text, obj.options[i].value, obj.options[i].defaultSelected, obj.options[i].selected) ;
113
if (o.length==0) { return; }
116
if ((a.text+"") < (b.text+"")) { return -1; }
117
if ((a.text+"") > (b.text+"")) { return 1; }
122
for (var i=0; i<o.length; i++) {
123
obj.options[i] = new Option(o[i].text, o[i].value, o[i].defaultSelected, o[i].selected);
127
// -------------------------------------------------------------------
128
// selectAllOptions(select_object)
129
// This function takes a select box and selects all options (in a
130
// multiple select object). This is used when passing values between
131
// two select boxes. Select all options in the right box before
132
// submitting the form so the values will be sent to the server.
133
// -------------------------------------------------------------------
134
function selectAllOptions(obj) {
135
if (!hasOptions(obj)) { return; }
136
for (var i=0; i<obj.options.length; i++) {
137
obj.options[i].selected = true;
141
// -------------------------------------------------------------------
142
// moveSelectedOptions(select_object,select_object[,autosort(true/false)[,regex]])
143
// This function moves options between select boxes. Works best with
144
// multi-select boxes to create the common Windows control effect.
145
// Passes all selected values from the first object to the second
146
// object and re-sorts each box.
147
// If a third argument of 'false' is passed, then the lists are not
148
// sorted after the move.
149
// If a fourth string argument is passed, this will function as a
150
// Regular Expression to match against the TEXT or the options. If
151
// the text of an option matches the pattern, it will NOT be moved.
152
// It will be treated as an unmoveable option.
153
// You can also put this into the <SELECT> object as follows:
154
// onDblClick="moveSelectedOptions(this,this.form.target)
155
// This way, when the user double-clicks on a value in one box, it
156
// will be transferred to the other (in browsers that support the
157
// onDblClick() event handler).
158
// -------------------------------------------------------------------
159
function moveSelectedOptions(from,to) {
160
// Unselect matching options, if required
161
if (arguments.length>3) {
162
var regex = arguments[3];
164
unSelectMatchingOptions(from,regex);
168
if (!hasOptions(from)) { return; }
169
for (var i=0; i<from.options.length; i++) {
170
var o = from.options[i];
172
if (!hasOptions(to)) { var index = 0; } else { var index=to.options.length; }
173
to.options[index] = new Option( o.text, o.value, false, false);
176
// Delete them from original
177
for (var i=(from.options.length-1); i>=0; i--) {
178
var o = from.options[i];
180
from.options[i] = null;
183
if ((arguments.length<3) || (arguments[2]==true)) {
187
from.selectedIndex = -1;
188
to.selectedIndex = -1;
191
// -------------------------------------------------------------------
192
// copySelectedOptions(select_object,select_object[,autosort(true/false)])
193
// This function copies options between select boxes instead of
194
// moving items. Duplicates in the target list are not allowed.
195
// -------------------------------------------------------------------
196
function copySelectedOptions(from,to) {
197
var options = new Object();
198
if (hasOptions(to)) {
199
for (var i=0; i<to.options.length; i++) {
200
options[to.options[i].value] = to.options[i].text;
203
if (!hasOptions(from)) { return; }
204
for (var i=0; i<from.options.length; i++) {
205
var o = from.options[i];
207
if (options[o.value] == null || options[o.value] == "undefined" || options[o.value]!=o.text) {
208
if (!hasOptions(to)) { var index = 0; } else { var index=to.options.length; }
209
to.options[index] = new Option( o.text, o.value, false, false);
213
if ((arguments.length<3) || (arguments[2]==true)) {
216
from.selectedIndex = -1;
217
to.selectedIndex = -1;
220
// -------------------------------------------------------------------
221
// moveAllOptions(select_object,select_object[,autosort(true/false)[,regex]])
222
// Move all options from one select box to another.
223
// -------------------------------------------------------------------
224
function moveAllOptions(from,to) {
225
selectAllOptions(from);
226
if (arguments.length==2) {
227
moveSelectedOptions(from,to);
229
else if (arguments.length==3) {
230
moveSelectedOptions(from,to,arguments[2]);
232
else if (arguments.length==4) {
233
moveSelectedOptions(from,to,arguments[2],arguments[3]);
237
// -------------------------------------------------------------------
238
// copyAllOptions(select_object,select_object[,autosort(true/false)])
239
// Copy all options from one select box to another, instead of
240
// removing items. Duplicates in the target list are not allowed.
241
// -------------------------------------------------------------------
242
function copyAllOptions(from,to) {
243
selectAllOptions(from);
244
if (arguments.length==2) {
245
copySelectedOptions(from,to);
247
else if (arguments.length==3) {
248
copySelectedOptions(from,to,arguments[2]);
252
// -------------------------------------------------------------------
253
// swapOptions(select_object,option1,option2)
254
// Swap positions of two options in a select list
255
// -------------------------------------------------------------------
256
function swapOptions(obj,i,j) {
258
var i_selected = o[i].selected;
259
var j_selected = o[j].selected;
260
var temp = new Option(o[i].text, o[i].value, o[i].defaultSelected, o[i].selected);
261
var temp2= new Option(o[j].text, o[j].value, o[j].defaultSelected, o[j].selected);
264
o[i].selected = j_selected;
265
o[j].selected = i_selected;
268
// -------------------------------------------------------------------
269
// moveOptionUp(select_object)
270
// Move selected option in a select list up one
271
// -------------------------------------------------------------------
272
function moveOptionUp(obj) {
273
if (!hasOptions(obj)) { return; }
274
for (i=0; i<obj.options.length; i++) {
275
if (obj.options[i].selected) {
276
if (i != 0 && !obj.options[i-1].selected) {
277
swapOptions(obj,i,i-1);
278
obj.options[i-1].selected = true;
284
// -------------------------------------------------------------------
285
// moveOptionDown(select_object)
286
// Move selected option in a select list down one
287
// -------------------------------------------------------------------
288
function moveOptionDown(obj) {
289
if (!hasOptions(obj)) { return; }
290
for (i=obj.options.length-1; i>=0; i--) {
291
if (obj.options[i].selected) {
292
if (i != (obj.options.length-1) && ! obj.options[i+1].selected) {
293
swapOptions(obj,i,i+1);
294
obj.options[i+1].selected = true;
300
// -------------------------------------------------------------------
301
// removeSelectedOptions(select_object)
302
// Remove all selected options from a list
303
// (Thanks to Gene Ninestein)
304
// -------------------------------------------------------------------
305
function removeSelectedOptions(from) {
306
if (!hasOptions(from)) { return; }
307
for (var i=(from.options.length-1); i>=0; i--) {
308
var o=from.options[i];
310
from.options[i] = null;
313
from.selectedIndex = -1;
316
// -------------------------------------------------------------------
317
// removeAllOptions(select_object)
318
// Remove all options from a list
319
// -------------------------------------------------------------------
320
function removeAllOptions(from) {
321
if (!hasOptions(from)) { return; }
322
for (var i=(from.options.length-1); i>=0; i--) {
323
from.options[i] = null;
325
from.selectedIndex = -1;
328
// -------------------------------------------------------------------
329
// addOption(select_object,display_text,value,selected)
330
// Add an option to a list
331
// -------------------------------------------------------------------
332
function addOption(obj,text,value,selected) {
333
if (obj!=null && obj.options!=null) {
334
obj.options[obj.options.length] = new Option(text, value, false, selected);