This is a jQuery plugin to replace the options in a select menu.
Note: These examples assume you have already included jQuery and this plugin.
$(function() {
var options = [
{ text: 'one', value: 1 },
{ text: 'two', value: 2 }
];
$('select').selectOptions(options);
});
$(function() {
var options = [
{ text: 'one', value: 1 },
{ text: 'two', value: 2 }
];
$('select').selectOptions(options, { selected: 2 });
});
$(function() {
var options = [
{ text: 'one', value: 1 },
{ text: 'two', value: 2 }
];
$('select').selectOptions(options, { includeBlank: 'choose something' });
});
$(function() {
var options = {
"Group 1": [
{ text: 'one', value: 1 },
{ text: 'two', value: 2}
],
"Group 2": [
{ text: 'three', value: 3 },
{ text: 'four', value: 4 }
]
};
$('select').selectOptions(options);
});
$(function() {
var options = {
"Group 1": [
{ text: 'one', value: 1 },
{ text: 'two', value: 2}
],
"Group 2": [
{ text: 'three', value: 3 },
{ text: 'four', value: 4 }
]
};
$('select').selectOptions(options, { includeBlank: 'choose something', selected: 3 });
});
I've chosen to write this plugin in Coffeescript and compile it for easy use. If you wish to contribute, please edit the Coffeescript file and compile it into JS before committing.
For directions on how to use Coffeescript, please reference http://coffeescript.org