Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery - Fire change event onload

Tags:

jquery

I've a question related to a select element and jquery. I currently have a change event so the changeShippingForm function gets called when the select element value has been changed, this is working just fine.

But I also want the change event fired at onload. How can I achieve this?

$(document).ready(function() {
    $('#shippingType').change(changeShippingForm);
});

function changeShippingForm() {
   // do some stuff
}
like image 808
tom Avatar asked Feb 03 '11 14:02

tom


1 Answers

use .trigger()help.

$(document).ready(function() {
   $('#shippingType').change(changeShippingForm).trigger('change');
});

or even shorter, use the shortcut:

$(document).ready(function() {
   $('#shippingType').change(changeShippingForm).change();
});
like image 195
jAndy Avatar answered Oct 24 '22 00:10

jAndy