I have an existing select:
var $selectStatus = jQuery("<select id='Status' name='status'></select>");
var enabledHtml = "<option value='ENABLED'>"ON"</option>";
var disabledHtml = "<option value='DISABLED'>"OFF"</option>";
var $statusEnabled = jQuery(enabledHtml);
var $statusDisabled = jQuery(disabledHtml);
//some code to add "selected" properties
...
//finally add to select
$selectStatus.append($statusEnabled, $statusDisabled);
This produces a select with ON/OFF as the options. Now I want to add an optgroup along with some values I've got stored in an array.
So what you should get is a select something like:
<select>
<option value="ENABLED">ON</option>
<option value="DISABLED">OFF</option>
<optgroup label="Info">
<option value="some value">some value</option>
<option value="some value">some value</option>
...
</optgroup>
</select>
It could be an empty optgroup with no option values or any number of option values.
I tried this:
var status_optgroup_open = "<optgroup label='Info'>"
for(i=0;i<info.length;i++)
{
$foobar = jQuery('#Status').append(jQuery('<option/>').val(info[i].info_name).html(info[i].info_name));
}
var status_optgroup_close = "</optgroup>"
var $statusOpen = jQuery(status_optgroup_open);
var $statusClose = jQuery(status_optgroup_close);
$selectStatus.append($statusEnabled, $statusDisabled, $statusOpen, $foobar, $statusClose);
The output of info[i].info_name is what I expect but I'm just not sure how do I append them in this context. Right now this just yields an empty optgroup.