Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

152
Views
Update other fields from data returned in ajax call using select2

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

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!