rpurant
rpurant

Reputation: 304

trigger an event on select options load on page load

I would like to get the list of business types based on selected industry types. I am using following code -

$('#industry_type').change(function () {
    var industry_id = $(this).val();
    getBusinessTypes(industry_id);
    return false;
});

function getBusinessTypes(industry_id) {
    $.ajax({
        url: baseURL + ('processes/org_process.php'),
        type: "POST",
        data: { 'action': 'all', 'industry_id': industry_id },
        success: function (data) {
            var business_type = $('#business_types');
            business_type.chosen('destroy');
            business_type.html(data);
            business_type.chosen({ width: "100%" });
        }
    });
}

Which will return me the list of business types.

Bu the problem here is it will fire the event only if on change of select options. If the select option already selected on page load then it does not trigger the event.

Any one have any idea how would I get the list of business type based on selected industry type on page load?

Thanks!

Upvotes: 2

Views: 50

Answers (1)

Gabriele Petrioli
Gabriele Petrioli

Reputation: 195992

Add .trigger('change') after binding the event handler.

$('#industry_type').change(function () {
    var industry_id = $(this).val();
    getBusinessTypes(industry_id);
    return false;
}).trigger('change');

Upvotes: 1

Related Questions