Ok I would like to be able to add another select box with the same options if the previous select box option is selected. If it’s de-selected then remove the last select box w/ options.
Here is what I’m trying, I can add/remove another select box with the first select box but I can’t with the newly created select box.
var counter = 1; // as we already have the first dropdown
function populate(selector) {
$(selector)
.append('<option value="foo">foo</option>')
.append('<option value="bar">bar</option>')
}
populate('#select_options_1');
$("#select_options_" + counter).change( function() {
var addNewSelect = $(this).val() != '';
if(addNewSelect) {
counter++;
var newSelectOption = $(document.createElement('select')).attr("id", 'select_options_' + counter);
newSelectOption.html('<option value="">Please Select an Option</option>' +
'<option value="foo">foo</option>' +
'<option value="bar">bar</option>');
newSelectOption.appendTo("#select_option_groups");
} else {
$('#select_options_' + counter).remove();
if(counter > 1) {
counter--;
}
}
alert('Add New Select? ' + addNewSelect + ' Counter ' + counter);
});
The HTML
<div name="select_option_groups" id="select_option_groups">
<select name="select_options_1" id="select_options_1">
<option value="">Please Select an Option</option>
</select>
</div>
Example Code from above: http://jsfiddle.net/RaHhY/31/
Here’s a shorter solution, not sure if you’re interested:
Given the following:
Demo
Update
So much for shorter code :giggle:. Anyways, this is an update with a few basic improvements: