231

I have a change event that is working fine but I need to get it to recurse.

So I have a function that is triggered on change that will "change" other drop downs based on a class selector (notice "drop downS", there could be more than one). This proxy change does not trigger the function and so fails. How can I get it to work?

Code

$(document).ready(function () {
    var activeDropBox = null;

    $("select.drop-box").change(function () {
        var questionId = $(this).attr("questionId");
        var selectedAnswer = $(this).val();
        activeDropBox = this;

        alert(this.questionId);

        $.ajax(
        {
            type: "POST",
            url: answerChangedActionUrl,
            data: { questionId: questionId, selectedValue: selectedAnswer },
            success: function (data) {
                SetElementVisibility(data.ShowElement, questionId);
            }, error: function (XMLHttpRequest, textStatus, errorThrown) {
                alert('XMLHttpRequest:' + XMLHttpRequest.responseText);
                alert('textStatus:' + textStatus);
                alert('errorThrown:' + errorThrown);
            }
        });
    });

    function SetElementVisibility(visible, questionId) {
        // I would like each child to then trigger the change event...
        $(".childOf" + questionId)[visible ? 'show' : 'hide']('slow');
        
        // Suggested code
        //$(".childOf" + questionId + " select").trigger("change");

        if (!visible) {
            $(".childOf" + questionId + " select").attr('selectedIndex', 0);
        }
    }
}

The suggestions so far seem to work, but as the change event triggers an ajax post it now seems to fail here. I'm going to play around with it but that is something for another question I feel.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
4imble
  • 13,979
  • 15
  • 70
  • 125
  • 1
    Provide some code so we can have a look – Alex Rashkov Nov 22 '10 at 15:51
  • How can we tell you how to get it to work, when you haven't shown us what *it* is? – user113716 Nov 22 '10 at 15:58
  • 1
    I thought it was a simple concept and didn't feel code was required. The answers so far seem to have understood my explanation and so I am trying their solutions now. If I don't have any joy I'll post some code. My implementation is actually much more complex. – 4imble Nov 22 '10 at 16:05
  • 1
    Sorted it, was a problem with me changing the value after the ajax post. Thanks all for the help. The suggestions posted worked like a charm. – 4imble Nov 22 '10 at 16:35

5 Answers5

501

Use the trigger() method

$(selector).trigger("change");
John Hartsock
  • 85,422
  • 23
  • 131
  • 146
53

For me

$('#element').val('...').change()

is the best way.

Note: .change() is deprecated in newer versions use .trigger('change') instead.

Amit Shah
  • 7,771
  • 5
  • 39
  • 55
  • Yeah!, I like this one – Juan Herrera May 14 '16 at 16:44
  • Yes! it allows to choose option on the same time. I think your answer deserves to be the best here. – Sergej Fomin Sep 19 '17 at 11:05
  • This is the only way to trigger change event when programmatically altering the element's value. – CodeMantle Jul 26 '20 at 09:45
  • 1
    this is now deprecated. all best function essentials for development are getting deprecated... is it to make the developer keep learning new things always? – Amit Shah Mar 24 '21 at 03:04
  • @AmitShah Yeah I think `.change()` is deprecated in newer versions but `.trigger('change')` can be used just as well. I think the jQuery team is tired of making shortcuts for functions. It is also easier to find the right function to call when the documentation is a lot smaller. – Peter Krebs Sep 14 '21 at 07:17
21

The parameterless form of the change() method triggers a change event. You can write something like:

$(document).ready(function() {
    $("#yourInitialElementID").change(function() {
        // Do something here...
        $(".yourDropDownClass").change();
    });
});
Frédéric Hamidi
  • 258,201
  • 41
  • 486
  • 479
14
$(selector).change()

.change()


.trigger("change")

Longer slower alternative, better for abstraction.

.trigger("change")

$(selector).trigger("change")
Geoffrey Hale
  • 10,597
  • 5
  • 44
  • 45
13

Use That :

$(selector).trigger("change");

OR

$('#id').trigger("click");

OR

$('.class').trigger(event);

Trigger can be any event that javascript support.. Hope it's easy to understandable to all of You.

Muhammad Fahad
  • 1,352
  • 15
  • 15