Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Force .change() function to run - jQuery

I have a .change() function set for a dropdown menu with an ID of #country. When the page loads, I'm trying to set the dropdown menu to "United States" and run the .change() function:

$('#country').change(function () {
    resetDisclosure();
    var countryCode = $(this).val();
    var countryName = $('#country option:selected').text();

    $('#'+countryCode.toString()).fadeIn('slow');

    if(countryCode == 'OC' || countryCode == 'EU') {
        $('#OC h4, #EU h4').html('For Residents of <strong>' + countryName + '</strong>');
    }

    $.fancybox.resize();
    $.fancybox.center();
});

$("#country").val('OC');
$("#country").change();

The last function there is wrong, because I can't force the .change() on load. How can I go about forcing the change function?

I'm super beginner and have tried to assign the contents of the .change() function to a different function and call that, but it didn't work either.

like image 845
Bill Addison Avatar asked Sep 27 '10 21:09

Bill Addison


1 Answers

Try:

$("#country").val('OC').trigger('change');

like image 63
prodigitalson Avatar answered Oct 03 '22 00:10

prodigitalson