'Triggering change event

I've got the following function:

$('#id_service').on('change', function() {

     afunction($(this));
            
});

I've then got another function that manually changes #id_service

$(document).ready(function() {

   $('#id_primary option:nth-child(2)').attr('selected', 'selected');
       
});

This function that sets the section option to select isn't triggering 'on change' function. How would I do this?

Thanks!



Solution 1:[1]

Maybe try using the jquery.trigger() function?

    $(document).ready(function() {
       $('#id_primary option:nth-child(2)').attr('selected', 'selected');
       $('#id_service').trigger('change');
    });

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Lord Virus