I have a working select2 field which updates based on the results of the ajax call, what i now want to do is up two other fields in my template using some of the data which is returned in the ajax call.
The below is the script so far
<script>
$(document).ready(function () {
$('#id_site').select2({
theme: 'bootstrap4',
placeholder: "Select a site",
ajax: {
url: '{% url 'site-view-ajax' %}',
dataType: 'json',
processResults: function (data) {
return {
results: $.map(data, function (item) {
return {id: item.id,
text: item.site_name
};
})
};
}
},
minimumInputLength: 3
});
});
</script>
What i then want to do is access two additional fields from the returned data and update my template.
<script>
$(document).ready(function () {
$('#id_site').select2({
theme: 'bootstrap4',
placeholder: "Select a site",
ajax: {
url: '{% url 'site-view-ajax' %}',
dataType: 'json',
processResults: function (data) {
return {
results: $.map(data, function (item) {
return {id: item.id,
text: item.site_name,
code: item.site_code,
address: item.site_address
};
})
};
}
},
minimumInputLength: 3
});
});
</script>
and then something like the below
document.getElementById("id_site_code").value = item.site_code;
document.getElementById("id_site_address").value = item.site_address;
Im just not sure how i would go about this as i have very limited javascript knowledge.
Thanks