J86
J86

Reputation: 15307

onChange event not firing when input changed dynamically

On my form, I have a DropDown, when a value is selected, I fire some JavaScript that populates some visible <input type="text" /> fields (under neath the drop down).

The problem is, I also want to run some JavaScript function whenever the value of one of these inputs changes, I want this function to run even if the text input is changed by the drop down selection.

Here is my current code:

jQuery(document).ready(function() {
    $('#mainPane').on('change', '#formElementFromEmail', function(event){
        var collectFromDropDown = $('#formElementFromEmail');
        persistHiddenInputForCollectFromValue();
    });
});

I suspect the change event is not being fired because no one has manually changed the value of the field in question. How can I overcome this?

Upvotes: 2

Views: 5950

Answers (2)

helion3
helion3

Reputation: 37501

Since you're using jQuery, you can trigger the event:

$('#mainPane').trigger('change');

Upvotes: 4

A.T.
A.T.

Reputation: 26382

you can simulate change event by

var element = document.getElementById('formElementFromEmail');
element.onchange();

Upvotes: 3

Related Questions