views:

47

answers:

1

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 ' + countryName + '');
    }

    $.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.

+2  A: 

Try:

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

prodigitalson
He's already doing this effectively, `.change()` *is* `.trigger('change')` it's just a shortcut.
Nick Craver
Thanks, the trigger function looks perfect, but it posts an error still.
Bill Addison
I think its got something to do with the fact that the dropdown being changed is in a Fancybox that is being called at the same time as the change?
Bill Addison
@nick: true, but when a shortcut doesnt it never hurts to call verbose version.. hence the "try" :-) @bill: this is likely not an issue unless the box is hidden.. as long as its visible (ie. `display:block;`) that shouldnt interfere. what error are you getting?
prodigitalson
@prodigitalson - Show me a situation where `.change()` doesn't work and `.trigger('change')` does (or any other handler) then I'll concede that point...until then I'll consider it a mis-use of debugging time :)
Nick Craver
@nick: well, hell if i can think of one :-)
prodigitalson