Currently, I am attempting to utilize select2 with jQuery 1.9.1 along with select2 3.3.0 for a relatively standard use case. My goal is to fill a select2 control with choices based on the status of another select2 control.
This is what I have:
<div>
<select id="master" name="master">
<option value=1>a</option>
<option value=2>b</option>
</select>
</div>
<div>
<input type="hidden" id="detail" name="detail" data-placeholder="No options until master chosen">
<script src="jquery-1.9.1.js"></script>
<script src="select2-3.3.0/select2.js"></script>
<script>
var a = [];
var b = [];
for (var i = 0; i < 20; i++) {
a.push({id: i, text: "a" + i});
b.push({id: i, text: "b"+ i});
}
$(document).ready( function () {
$("#master").select2().on('change', function() {
console.log("master changed: " + $("#master").val())
var right_one = a;
if($("#master").val() == 2) right_one = b;
$("#detail").removeClass('select2-offscreen').select2({data:right_one})
console.log("right_one: " + right_one)
})
})
Edit: This answer explains why the control was disappearing. However, I still have questions regarding this setup:
- Is there a way to prevent #master from having an initial selection?
- How can I modify the placeholder text in #detail to reflect the fact that a master option has been selected?