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#.
If you are trying to have linked drop downs, the best way to do it is to have a script that returns the a prebuilt select box and an AJAX call that requests it.
Here is the documentation for jQuery's Ajax method if you need it.
Then have a span around your state select box with the id of "stateBoxHook"
Try this:
Define the change event, and trigger it immediately. This ensures the event handler is defined before calling it.
Might be late to answer the original poster, but someone else might benefit from the shorthand notation, and this follows jQuery's chaining, etc
jquery chaining
Try this:
$('#id').change();
Works for me.
On one line together with setting the value:
$('#id').val(16).change();
I don't know that much JQuery but I've heard it allows to fire native events with this syntax.
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.
alternatively you can put onchange attribute on the dropdownlist itself, that onchange will call certain jquery function like this.
hope this one helps you, and please note that this code is just a rough draft, not tested on any ide. thanks