3 * Copyright (c) 2006-2008 Sam Collett (http://www.texotela.co.uk)
4 * Dual licensed under the MIT (http://www.opensource.org/licenses/mit-license.php)
5 * and GPL (http://www.opensource.org/licenses/gpl-license.php) licenses.
8 * Demo: http://www.texotela.co.uk/code/jquery/select/
18 * Adds (single/multiple) options to a select box (or series of select boxes)
21 * @author Sam Collett (http://www.texotela.co.uk)
23 * @example $("#myselect").addOption("Value", "Text"); // add single value (will be selected)
24 * @example $("#myselect").addOption("Value 2", "Text 2", false); // add single value (won't be selected)
25 * @example $("#myselect").addOption({"foo":"bar","bar":"baz"}, false); // add multiple values, but don't select
28 $.fn.addOption = function()
30 var add = function(el, v, t, sO)
32 var option = document.createElement("option");
38 option.selected = true;
43 if(a.length == 0) return this;
44 // select option when added? default is true
50 if(typeof(a[0]) == "object")
57 if(typeof(a[1]) == "boolean") sO = a[1];
58 else if(typeof(a[2]) == "boolean") sO = a[2];
68 if(this.nodeName.toLowerCase() != "select") return;
71 for(var item in items)
73 add(this, item, items[item], sO);
86 * Add options via ajax
89 * @author Sam Collett (http://www.texotela.co.uk)
91 * @param String url Page to get options from (must be valid JSON)
92 * @param Object params (optional) Any parameters to send with the request
93 * @param Boolean select (optional) Select the added options, default true
94 * @param Function fn (optional) Call this function with the select object as param after completion
95 * @param Array args (optional) Array with params to pass to the function afterwards
96 * @example $("#myselect").ajaxAddOption("myoptions.php");
97 * @example $("#myselect").ajaxAddOption("myoptions.php", {"code" : "007"});
98 * @example $("#myselect").ajaxAddOption("myoptions.php", {"code" : "007"}, false, sortoptions, [{"dir": "desc"}]);
101 $.fn.ajaxAddOption = function(url, params, select, fn, args)
103 if(typeof(url) != "string") return this;
104 if(typeof(params) != "object") params = {};
105 if(typeof(select) != "boolean") select = true;
114 $(el).addOption(r, select);
115 if(typeof fn == "function")
117 if(typeof args == "object")
134 * Removes an option (by value or index) from a select box (or series of select boxes)
137 * @author Sam Collett (http://www.texotela.co.uk)
139 * @param String|RegExp|Number what Option to remove
140 * @param Boolean selectedOnly (optional) Remove only if it has been selected (default false)
141 * @example $("#myselect").removeOption("Value"); // remove by value
142 * @example $("#myselect").removeOption(/^val/i); // remove options with a value starting with 'val'
143 * @example $("#myselect").removeOption(/./); // remove all options
144 * @example $("#myselect").removeOption(/./, true); // remove all options that have been selected
145 * @example $("#myselect").removeOption(0); // remove by index
146 * @example $("#myselect").removeOption(["myselect_1","myselect_2"]); // values contained in passed array
149 $.fn.removeOption = function()
152 if(a.length == 0) return this;
153 var ta = typeof(a[0]);
155 // has to be a string or regular expression (object in IE, function in Firefox)
156 if(ta == "string" || ta == "object" || ta == "function" )
159 // if an array, remove items
160 if(v.constructor == Array)
163 for(var i = 0; i<l; i++)
165 this.removeOption(v[i], a[1]);
170 else if(ta == "number") index = a[0];
175 if(this.nodeName.toLowerCase() != "select") return;
176 // does the option need to be removed?
179 var o = this.options;
182 // get number of options
184 for(var i=oL-1; i>=0; i--)
186 if(v.constructor == RegExp)
188 if(o[i].value.match(v))
193 else if(o[i].value == v)
197 // if the option is only to be removed if selected
198 if(remove && a[1] === true) remove = o[i].selected;
208 // only remove if selected?
211 remove = o[index].selected;
228 * Sort options (ascending or descending) in a select box (or series of select boxes)
231 * @author Sam Collett (http://www.texotela.co.uk)
233 * @param Boolean ascending (optional) Sort ascending (true/undefined), or descending (false)
234 * @example // ascending
235 * $("#myselect").sortOptions(); // or $("#myselect").sortOptions(true);
236 * @example // descending
237 * $("#myselect").sortOptions(false);
240 $.fn.sortOptions = function(ascending)
242 var a = typeof(ascending) == "undefined" ? true : !!ascending;
246 if(this.nodeName.toLowerCase() != "select") return;
248 var o = this.options;
249 // get number of options
251 // create an array for sorting
253 // loop through options, adding to sort array
254 for(var i = 0; i<oL; i++)
261 // sort items in array
265 // option text is made lowercase for case insensitive sorting
266 o1t = o1.t.toLowerCase(), o2t = o2.t.toLowerCase();
267 // if options are the same, no sorting is needed
268 if(o1t == o2t) return 0;
271 return o1t < o2t ? -1 : 1;
275 return o1t > o2t ? -1 : 1;
279 // change the options to match the sort array
280 for(var i = 0; i<oL; i++)
282 for (var j = i; j<oL; j++) {
283 if (o[j].text == sA[i].t && o[j].value == sA[i].v) {
285 $(o[i]).before(o[j]);
295 * Selects an option by value
297 * @name selectOptions
298 * @author Mathias Bank (http://www.mathias-bank.de), original function
299 * @author Sam Collett (http://www.texotela.co.uk), addition of regular expression matching
301 * @param String|RegExp value Which options should be selected
302 * can be a string or regular expression
303 * @param Boolean clear Clear existing selected options, default false
304 * @example $("#myselect").selectOptions("val1"); // with the value 'val1'
305 * @example $("#myselect").selectOptions(/^val/i); // with the value starting with 'val', case insensitive
308 $.fn.selectOptions = function(value, clear)
311 var vT = typeof(value);
312 var c = clear || false;
313 // has to be a string or regular expression (object in IE, function in Firefox)
314 if(vT != "string" && vT != "function" && vT != "object") return this;
318 if(this.nodeName.toLowerCase() != "select") return this;
320 var o = this.options;
321 // get number of options
323 for(var i = 0; i<oL; i++)
325 if(v.constructor == RegExp)
327 if(o[i].value.match(v))
329 o[i].selected = true;
333 o[i].selected = false;
340 o[i].selected = true;
344 o[i].selected = false;
354 * Copy options to another select
357 * @author Sam Collett (http://www.texotela.co.uk)
359 * @param String to Element to copy to
360 * @param String which (optional) Specifies which options should be copied - 'all' or 'selected'. Default is 'selected'
361 * @example $("#myselect").copyOptions("#myselect2"); // copy selected options from 'myselect' to 'myselect2'
362 * @example $("#myselect").copyOptions("#myselect2","selected"); // same as above
363 * @example $("#myselect").copyOptions("#myselect2","all"); // copy all options from 'myselect' to 'myselect2'
366 $.fn.copyOptions = function(to, which)
368 var w = which || "selected";
369 if($(to).size() == 0) return this;
373 if(this.nodeName.toLowerCase() != "select") return this;
375 var o = this.options;
376 // get number of options
378 for(var i = 0; i<oL; i++)
380 if(w == "all" || (w == "selected" && o[i].selected))
382 $(to).addOption(o[i].value, o[i].text);
391 * Checks if a select box has an option with the supplied value
393 * @name containsOption
394 * @author Sam Collett (http://www.texotela.co.uk)
395 * @type Boolean|jQuery
396 * @param String|RegExp value Which value to check for. Can be a string or regular expression
397 * @param Function fn (optional) Function to apply if an option with the given value is found.
398 * Use this if you don't want to break the chaining
399 * @example if($("#myselect").containsOption("val1")) alert("Has an option with the value 'val1'");
400 * @example if($("#myselect").containsOption(/^val/i)) alert("Has an option with the value starting with 'val'");
401 * @example $("#myselect").containsOption("val1", copyoption).doSomethingElseWithSelect(); // calls copyoption (user defined function) for any options found, chain is continued
404 $.fn.containsOption = function(value, fn)
410 // has to be a string or regular expression (object in IE, function in Firefox)
411 if(vT != "string" && vT != "function" && vT != "object") return fT == "function" ? this: found;
415 if(this.nodeName.toLowerCase() != "select") return this;
416 // option already found
417 if(found && fT != "function") return false;
419 var o = this.options;
420 // get number of options
422 for(var i = 0; i<oL; i++)
424 if(v.constructor == RegExp)
426 if (o[i].value.match(v))
429 if(fT == "function") fn.call(o[i], i);
437 if(fT == "function") fn.call(o[i], i);
443 return fT == "function" ? this : found;
447 * Returns values which have been selected
449 * @name selectedValues
450 * @author Sam Collett (http://www.texotela.co.uk)
452 * @example $("#myselect").selectedValues();
455 $.fn.selectedValues = function()
458 this.find("option:selected").each(
461 v[v.length] = this.value;
468 * Returns options which have been selected
470 * @name selectedOptions
471 * @author Sam Collett (http://www.texotela.co.uk)
473 * @example $("#myselect").selectedOptions();
476 $.fn.selectedOptions = function()
478 return this.find("option:selected");