//TEST var select_box_cache_list = new Array(); /* * * Copyright (c) 2006 Sam Collett (http://www.texotela.co.uk) * Licensed under the MIT License: * http://www.opensource.org/licenses/mit-license.php * * Addepted to select an option by Mathias Bank (http://www.mathias-bank.de) */ /* * Adds (single/multiple) options to a select box (or series of select boxes) * * @name addOption * @author Sam Collett (http://www.texotela.co.uk) * @example $("#myselect").addOption("Value", "Text"); // add single value (will be selected) * $("#myselect").addOption("Value 2", "Text 2", false); // add single value (won't be selected) * $("#myselect").addOption({"foo":"bar","bar":"baz"}, false); // add multiple values, but don't select * */ jQuery.fn.addOption = function() { if(arguments.length == 0) return this; // select option when added? default is true var selectOption = true; // multiple items var multiple = false; if(typeof arguments[0] == "object") { multiple = true; var items = arguments[0]; } if(arguments.length >= 2) { if(typeof arguments[1] == "boolean") selectOption = arguments[1]; else if(typeof arguments[2] == "boolean") selectOption = arguments[2]; if(!multiple) { var value = arguments[0]; var text = arguments[1]; } } this.each( function() { if(this.nodeName.toLowerCase() != "select") return; if(multiple) { for(var v in items) { jQuery(this).addOption(v, items[v], selectOption); } } else { var option = document.createElement("option"); option.value = value; option.text = text; this.options.add(option); } if(selectOption) { //this.options[this.options.length-1].selected = true; this.options[this.options.length-1].selected = 'selected'; } } ) return this; } /* * Removes an option (by value or index) from a select box (or series of select boxes) * * @name removeOption * @author Sam Collett (http://www.texotela.co.uk) * @example jQuery("#myselect").removeOption("Value"); // remove by value * jQuery("#myselect").removeOption(0); // remove by index * */ jQuery.fn.removeOption = function() { if(arguments.length == 0) return this; if(typeof arguments[0] == "string") var value = arguments[0]; else if(typeof arguments[0] == "number") var index = arguments[0]; else return this; this.each( function() { if(this.nodeName.toLowerCase() != "select") return; if(value) { var optionsLength = this.options.length; for(var i=optionsLength-1; i>=0; i--) { if(this.options[i].value == value) { this.options[i] = null; } } } else { this.remove(index); } } ) return this; } /* * Sort options (ascending or descending) in a select box (or series of select boxes) * * @name sortOptions * @author Sam Collett (http://www.texotela.co.uk) * @param ascending Sort ascending (true/undefined), or descending (false) * @example // ascending * jQuery("#myselect").sortOptions(); // or jQuery("#myselect").sortOptions(true); * // descending * jQuery("#myselect").sortOptions(false); * */ jQuery.fn.sortOptions = function(ascending) { this.each( function() { if(this.nodeName.toLowerCase() != "select") return; // default sort is ascending if parameter is undefined ascending = typeof ascending == "undefined" ? true : ascending; // get number of options var optionsLength = this.options.length; // create an array for sorting var sortArray = []; // loop through options, adding to sort array for(var i = 0; i option2text ? -1 : 1; } } ); // change the options to match the sort array for(var i = 0; i0;) { if (items[i].tagName == "undefined" || items[i].tagName == null) { }else if (items[i].tagName.toLowerCase() == "option") { this.removeChild(items[i]); }else if (items[i].tagName == "undefined") { this.removeChild(this.childNodes[i]); } } var cache_list = select_box_cache_list[this.id]; for(d in cache_list) { var selected = (cache_list[d].value == original_selected_value); //alert (cache_list[d].value + " is " + selected); $(this).addOption(cache_list[d].value, cache_list[d].text, selected); } } /** * Remove items that are not needed. */ var nodes = this.childNodes; //alert("The value is " + this.value); for (i=nodes.length;i-->0;) { if (items[i].tagName == "undefined" || items[i].tagName == null) { }else if (nodes[i].tagName.toLowerCase() == "option") { if (parseInt(value) >= parseInt(nodes[i].value)) { //Make sure we know whether it is selected //alert(nodes[i].value + " has selected " + nodes[i].selected); this.removeChild(nodes[i]); } }else if (items[i].tagName == "undefined") { //Make sure we know whether it is selected //alert(nodes[i].value + " has selected " + nodes[i].selected); this.removeChild(this.childNodes[i]); } } //Obscure IE7 Bug Fix var tmp = this.id; this.id = ""; this.id = tmp; $(this).addOption("TEMP", "", false); $(this).removeOption("TEMP"); } ) return this; }