Revision: 6075
Initial Code
Initial URL
Initial Description
Initial Title
Initial Tags
Initial Language
at April 24, 2008 17:47 by jeffreality
Initial Code
function changeNumericSequence (objSelectChosen) { // ------------------------------------------------------------------------------ // Created: Jeffrey B. Berthiaume // Date: 12 December 2000 // Version: 1.0 // Description: changeNumericSequence manipulates numbers in a series of dropdown // select lists. If there are three dropdown select lists (each with the options // of "1", "2", and "3") in a numerical sequence, this function will change the // other select lists as appropriate. // Each select list in the html form should have the name "cat_" + a common group // name for the group of select lists + a unique identifier. // for example: // <select name="cat_1_item_32" onChange="changeNumericSequence (this);"> // Parameter: // objSelectChosen (should be the select object that was just changed) // Usage example: // <select name="cat_1_item_32" onChange="changeNumericSequence (this);"> // ------------------------------------------------------------------------------ // find out which group of select lists (category) are being manipulated strSelectChosenName = objSelectChosen.name; arrSelectChosenName = strSelectChosenName.split ("_"); // strCategoryName contains the name of the category (i.e. the n in "cat_n") strCategoryName = arrSelectChosenName [1]; // find total number of items to manipulate intNumItems = objSelectChosen.length; // store the changed number (new number chosen for this dropdown) intChangedItemNumber = objSelectChosen.selectedIndex; arrItemNumbers = new Array (intNumItems); arrSelectNames = new Array (intNumItems); // go through and build array of all numbers in select list // increment through all select lists in the parent form of objSelectChosen // if their name begins with "cat_" + strCategoryName then they belong to this group of categories intTotalFormElements = objSelectChosen.form.elements.length intArrTotal = 0; for (i = 0; i < intTotalFormElements; i++) { strElementName = objSelectChosen.form.elements [i].name; arrSelectName = strElementName.split ("_"); if (strCategoryName == arrSelectName [1]) { if (strElementName == strSelectChosenName) { intChangedItemIndex = intArrTotal; } arrSelectNames [intArrTotal] = strElementName; arrItemNumbers [intArrTotal] = objSelectChosen.form.elements [i].selectedIndex; intArrTotal = intArrTotal + 1; } } // calculate the number that is missing intMissingNum = -1; i = 0; while (intMissingNum == -1) { boolNumFound = -1; for (i2 = 0; i2 < intNumItems; i2++) { if (arrItemNumbers [i2] == i) { boolNumFound = 0; } } if (boolNumFound == -1) { intMissingNum = i; } i++; } // re-order the numbers // the current item was intMissingNum // but now it's intChangedItemNumber if (intMissingNum < intChangedItemNumber) { // find all numbers between (intMissingNum + 1) and intChangedItemNumber for (i = 0; i < intNumItems; i++) { num = arrItemNumbers [i]; if ((i != intChangedItemIndex) && (num >= (intMissingNum + 1)) && (num <= intChangedItemNumber)) { // decrement their indices by 1 arrItemNumbers [i] = arrItemNumbers [i] - 1; } } } else { // find all numbers between (intChangedItemNumber + 1) and intMissingNum for (i = 0; i < intNumItems; i++) { num = arrItemNumbers [i]; if ((i != intChangedItemIndex) && (num >= intChangedItemNumber) && (num < intMissingNum)) { // increment their indices by 1 arrItemNumbers [i] = arrItemNumbers [i] + 1; } } } // change the selection drop boxes to be the newly ordered numbers for (i = 0; i < intNumItems; i++) { eval ("document." + objSelectChosen.form.name + "." + arrSelectNames [i] + ".selectedIndex = " + arrItemNumbers [i] + ";") } }
Initial URL
Initial Description
When you have a list of items to sort (say, three items, each with a drop-down from 1-3), this script will (when one of the item drop-downs is changed) automatically change the others to have appropriate numbers. For example, if your list is numbered: 1, 2, 3...and you change the second item to 3, the list will then be numbered 1, 3, 2.
Initial Title
Auto-resort numbers in selectable numbered list
Initial Tags
Initial Language
JavaScript