I can't get .trigger("change")
to work. Anyone know why?
jQuery(document).ready(function () {
jQuery("select[id='DROPDOWNID']").change(function () {
var selectedIndex = jQuery("select[id='DROPDOWNID']").prop('selectedIndex');
switch (selectedIndex) {
case 0:
hideVarforProspekt();
break;
case 1:
hideOrdervarde();
break;
case 2:
break;
case 3:
hideSektor();
break;
}
});
** jQuery("select[id='DROPDOWNID']").trigger("change"); **
function hideVarforProspekt() {
jQuery("input[id='IDFROMSHAREPOINT']").closest('tr').hide();
}
function hideSektor() {
jQuery("table[id='IDFROMSHAREPOINT']").closest('tr').hide();
}
function hideUppskOrder() {
jQuery("input[id='IDFROMSHAREPOINT']").closest('tr').hide();
}
});
jQuery change() Method The change event occurs when the value of an element has been changed (only works on <input>, <textarea> and <select> elements). The change() method triggers the change event, or attaches a function to run when a change event occurs.
val() doesn't trigger change() in jQuery.
The trigger() method is a method in jQuery which is used to trigger a specified event handler on selected element. Syntax: $(selector).trigger(event, param1, param2) Note: Extra parameters can be passed in trigger() method. Example 1: This method triggered two methods to increase the value of method.
A “trigger” is a business decision that can bring about change. In the context of organizational design, it would be an event that results in a company or department reorganization.
Change event handler should be defined before calling the .trigger('change')
or .change()
Below are 2 scenarios.
Scenario 1: where change event is called before it is defined.
$(document).ready(function()
{
$('#selector').change(); or $('#selector').trigger('change');
$('#selector').on('change', function(){
});
});
Scenario 2: where change event handler is defined before calling it
$(document).ready(function()
{
$('#selector').on('change', function(){
});
$('#selector').change(); or $('#selector').trigger('change');
});
For me issue was fixed when I use scenario2. Hope this helps!!
Sometimes the usage of trigger is not necessary:
// use just jQuery("#DROPDOWNID") instead
var select = jQuery("select[id='DROPDOWNID']");
// placing the handler in separate function
var changeHandler = function () {
var selectedIndex = select.prop('selectedIndex');
switch(selectedIndex) {
case 0:
hideVarforProspekt();
break;
case 1:
hideOrdervarde();
break;
case 2:
break;
case 3:
hideSektor();
break;
}
}
// cache your jQuery selectors. It's a good practice
// and improves the readability
select.change(changeHandler);
// triggering
changeHandler();
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