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#.
This question is related to
jquery
asp.net-mvc
triggers
Try this:
$(document).ready(function(event) {
$('#countrylist').change(function(e){
// put code here
}).change();
});
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
Try this:
$('#id').change();
Works for me.
On one line together with setting the value:
$('#id').val(16).change();
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.
$(document).ready(function(){
$('#countrylist').change(function(e){
$this = $(e.target);
$.ajax({
type: "POST",
url: "scriptname.asp", // Don't know asp/asp.net at all so you will have to do this bit
data: { country: $this.val() },
success:function(data){
$('#stateBoxHook').html(data);
}
});
});
});
Then have a span around your state select box with the id of "stateBoxHook"
For some reason, the other jQuery
solutions provided here worked when running the script from console, however, it did not work for me when triggered from Chrome Bookmarklets.
Luckily, this Vanilla JS solution (the triggerChangeEvent
function) did work:
/**_x000D_
* Trigger a `change` event on given drop down option element._x000D_
* WARNING: only works if not already selected._x000D_
* @see https://stackoverflow.com/questions/902212/trigger-change-event-of-dropdown/58579258#58579258_x000D_
*/_x000D_
function triggerChangeEvent(option) {_x000D_
// set selected property_x000D_
option.selected = true;_x000D_
_x000D_
// raise event on parent <select> element_x000D_
if ("createEvent" in document) {_x000D_
var evt = document.createEvent("HTMLEvents");_x000D_
evt.initEvent("change", false, true);_x000D_
option.parentNode.dispatchEvent(evt);_x000D_
}_x000D_
else {_x000D_
option.parentNode.fireEvent("onchange");_x000D_
}_x000D_
}_x000D_
_x000D_
// ################################################_x000D_
// Setup our test case_x000D_
// ################################################_x000D_
_x000D_
(function setup() {_x000D_
const sel = document.querySelector('#fruit');_x000D_
sel.onchange = () => {_x000D_
document.querySelector('#result').textContent = sel.value;_x000D_
};_x000D_
})();_x000D_
_x000D_
function runTest() {_x000D_
const sel = document.querySelector('#selector').value;_x000D_
const optionEl = document.querySelector(sel);_x000D_
triggerChangeEvent(optionEl);_x000D_
}
_x000D_
<select id="fruit">_x000D_
<option value="">(select a fruit)</option>_x000D_
<option value="apple">Apple</option>_x000D_
<option value="banana">Banana</option>_x000D_
<option value="pineapple">Pineapple</option>_x000D_
</select>_x000D_
_x000D_
<p>_x000D_
You have selected: <b id="result"></b>_x000D_
</p>_x000D_
<p>_x000D_
<input id="selector" placeholder="selector" value="option[value='banana']">_x000D_
<button onclick="runTest()">Trigger select!</button>_x000D_
</p>
_x000D_
alternatively you can put onchange attribute on the dropdownlist itself, that onchange will call certain jquery function like this.
<input type="dropdownlist" onchange="jqueryFunc()">
<script type="text/javascript">
$(function(){
jqueryFunc(){
//something goes here
}
});
</script>
hope this one helps you, and please note that this code is just a rough draft, not tested on any ide. thanks
Source: Stackoverflow.com