Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You can store the original order in an array, and when inserting back, determine what's the latest element in the array that precedes the one to be inserted AND matches what's currently in the select list. Then insert after that.</p> <p>A better solution is to just store the old array whole and re-populate on every insertion with desired elements as follows (warning: code not tested)</p> <pre><code>function init(selectId) { var s = document.getElementById(selectId); select_defaults[selectId] = []; select_on[selectId] = []; for (var i = 0; i &lt; s.options.length; i++) { select_defaults[selectId][i] = s.options[i]; select_on[selectId][i] = 1; var value = list.options[i].value; select_map_values[selectId][value] = i if you wish to add/remove by value. var id = list.options[i].id; // if ID is defined for all options select_map_ids[selectId][id] = i if you wish to add/remove by id. } } function switch(selectId, num, id, value, to_add) { // You can pass number, value or id if (num == null) { if (id != null) { num = select_map_ids[selectId][id]; // check if empty? } else { num = select_map_values[selectId][value]; // check if empty? } } var old = select_on[selectId][num]; var newOption = (to_add) : 1 : 0; if (old != newOption) { select_on[selectId][num] = newOption; redraw(selectId); } } function add(selectId, num, id, value) { switch(selectId, num, id, value, 1); } function remove(selectId, num, id, value) { switch(selectId, num, id, value, 0); } function redraw(selectId) { var s = document.getElementById(selectId); s.options.length = 0; // empty out for (var i = 0; i &lt; select_on[selectId].length; i++) { // can use global "initial_length" stored in init() instead of select_on[selectId].length if (select_on[selectId][i] == 1) { s.options.push(select_defaults[selectId][i]); } } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload