Detect change in chosen field?

126 views
Skip to first unread message

René Kreijveld

unread,
Mar 1, 2021, 8:28:37 AM3/1/21
to Joomla! General Development
Hi all,

I have a backend component where I have an edit form.
The first field is al select list where I can choose a project. This field is rendered with Chosen.

I want to detect when a user choose a project in the select list so I can update to other fields. I have tried a jQuery change on the original field and also tried to detect a change() on the chosen field. But that's not wordking. Does anyone have a suggestion for me?
Message has been deleted

René Kreijveld

unread,
Mar 2, 2021, 3:05:43 AM3/2/21
to Joomla! General Development
To answer my own question:
In my specific case I have projects with either one or two companies linked to every project.
In my form I can add a downloadable file to the project, but the download is for only one company.
So, in my form I have a select list where I choose the project first.
After I have chosen the project, I do an Ajax call retrieve the companies linked to the project and I insert these into the second field of the form, the select list where I can choose the company.
The JavaScript code to update the second select list (jform_company) is as follows:

jQuery(document).ready(function ($) {
    $('#jform_project').on('change', function(evt, params) {
        var pid = params.selected;
        var url = "/media/com_docsafe/ajax/getcompanies.php?pid=" + pid;
        $.getJSON(url, function(data) {
            $("#jform_company").empty();
            var newcompany = $("<option value=''>- Choose -</option>");
            $("#jform_company").append(newcompany);
            if (data.company1id) {
                var newcompany = $("<option value='" + data.company1id + "'>" + data.company1name + "</option>");
                $("#jform_company").append(newcompany);
            }
            if (data.company2id) {
                var newcompany = $("<option value='" + data.company2id + "'>" + data.company2name + "</option>");
                $("#jform_company").append(newcompany);
            }
            $('#jform_company').trigger('liszt:updated');
            $('#jform_company').trigger('chosen:updated');
        });
    });
});

Op maandag 1 maart 2021 om 14:28:37 UTC+1 schreef René Kreijveld:
Reply all
Reply to author
Forward
0 new messages