This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function indexOf(array, value) { | |
for (var i = 0, l = array.length; i < l; ++i) { | |
if (array[i] === value) return i; | |
} | |
return -1; | |
} |
Now the code to set the selections. You pass in the select element, an array of selections, and an optional boolean. The boolean defaults to false and indicates whether or not to invert the selection:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function setSelectedByValue(select, values, invert) { | |
invert = invert || false; | |
var options = select.options; | |
for (var i = 0, l = options.length; i < l; ++i) { | |
var option = options[i]; | |
var value = option.value; | |
option.selected = (indexOf(values, value) === -1) === invert; | |
} | |
} |
Now for a little demo: