I want to trigger the change event of dropdown in $(document).ready using jquery.
I have a cascading dropdown for country and state in user details page. how can i set the value (which is taken from DB based on the user id) for country and state in MVC with C#.
$(document). ready(function(){ $('#countrylist'). change(function(e){ // Your event handler }); // And now fire change event when the DOM is ready $('#countrylist'). trigger('change'); });
I don't know that much JQuery but I've heard it allows to fire native events with this syntax.
$(document).ready(function(){ $('#countrylist').change(function(e){ // Your event handler }); // And now fire change event when the DOM is ready $('#countrylist').trigger('change'); });
You must declare the change event handler before calling trigger() or change() otherwise it won't be fired. Thanks for the mention @LenielMacaferi.
More information here.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With