Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery .trigger("change") not working

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();
    }
});
like image 476
Robin Avatar asked Sep 09 '13 07:09

Robin


People also ask

How to manually trigger a change event in jQuery?

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.

Does jQuery Val trigger change?

val() doesn't trigger change() in jQuery.

What is 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.

What is trigger change?

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.


2 Answers

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!!

like image 93
Nilesh Avatar answered Oct 12 '22 09:10

Nilesh


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();
like image 25
Krasimir Avatar answered Oct 12 '22 09:10

Krasimir