1

On page load I need to call change events for the drop down list. Currently what happens is in my Jquery code below when you select a value from the professions drop down list i.e. GP a Help text then below the drop down shows in a DIV area shows, however once you submit the form and if validation is thrown the help text goes from the view.

I need to call change events for the drop down list #profession but not exactly sure how to do this. Please advise

    $('#profession').on('change', function (e) { //Gets the ID of profession drop down list
        var selectedVal = $(this).val(); //Variable selectedVal this . value
        $.ajax({ //Ajax declared
            type: 'GET', //Its a get
            url: "@Url.Action("GetenquiryTypes", "UnauthEnquiry")", //It goes to the enquiry controller method GetenquiryTypes
            dataType: 'json', //Datatypes JSON
            data: { SelectedProfession: selectedVal }, //data is SelectedProfession: selectedVal
            success: function (json) { //Jquery Parse Json data from server on ajax success
                if (json.helptext != undefined && json.helptext != '')
                    {
                    $('#ProfHelp').html(json.helptext)
                    $('#ProfHelpAlert').show(); ///////
                }
                else
                    $('#ProfHelpAlert').hide(); ///////

                var targetDropdown = $('#enquirytype') //Var targetDropDropdown goes to dropdown ID enquiry type
                targetDropdown.empty(); //target empty dropdown
                $("<option />", {
                    val: "",
                    text: "Please select enquiry type" //Select enquiry type
                }).appendTo(targetDropdown); //add to the target dd
                if (json.enquiryTypes.length > 0) { //if JASON data from server greater then 0
                    for (var EnquiryType in json.enquiryTypes) { //go through each EnquiryType in JSON
                        $("<option />", {
                            val: json.enquiryTypes[EnquiryType].EnquiryId, //mapping
                            text: json.enquiryTypes[EnquiryType].Enquiryname //mapping
                        }).appendTo(targetDropdown); //add to drop down
                    };
                }
                targetDropdown.change();
            }
        });
    });
  • Possible duplicate of [Trigger change event of dropdown](http://stackoverflow.com/questions/902212/trigger-change-event-of-dropdown) – Sr Julien Oct 03 '16 at 16:24

1 Answers1

0

Try this:

$(document).ready(function() {
    $('#profession').trigger('change');
});
Sr Julien
  • 494
  • 1
  • 8
  • 27