I am successfully populating a select2 (v4.0.1) dropdown with dynamically (via AJAX) retrieved options.
Here is the HTML:
<select id="part-reference" class="select-dropdown" name="part-reference"></select>
Here is the Javascript:
$('#part-reference').select2({
ajax: {
url: function (params) {
return "parts/parts-search?nodecorator=true&input=" + params.term;
},
dataType: 'json',
type: "POST",
delay: 300,
processResults: function (data, params) {
return {
results: data.results
};
},
cache: true
}
});
The AJAX response looks like this:
{
"results": [
{
"id": 2,
"text": "Two"
},
{
"id": 3,
"text": "Three"
}
]
}
When I select one of the options in the dropdown the input field is not populated with the text, and when I submit the form the value for this select is undefined
.
What do I need to do so that when a dynamically added option is clicked, it populates the input field and sets its value?