Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Trigger change event of dropdown

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

like image 824
Prasad Avatar asked May 23 '09 18:05

Prasad


People also ask

How do you trigger an event on dropdown change?

$(document). ready(function(){ $('#countrylist'). change(function(e){ // Your event handler }); // And now fire change event when the DOM is ready $('#countrylist'). trigger('change'); });


1 Answers

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.

like image 101
Christophe Eblé Avatar answered Sep 20 '22 11:09

Christophe Eblé