'textbox value change event not getting fired jQuery

I am filling texbox value through ajax call. I want to change event get fired when the value gets filled.

ajax call:

$.ajax({
    url: 'ajaxExecute.aspx/GetCustInfoTeller',
    data: strRequest,
    dataType: "json",
    contentType: "application/json",
    cache: false,
    context: document.body,
    type: 'POST',
    
    success: function (response) {
        var saResponse = response.d.split("|");
        $('#txtDateofBirth').val(saResponse[0]); // For this textbox change event gets fired    
    }
});

Code for change event handler:

$("#txtDateofBirth").bind('change', function () {
    alert('1');
    var today = new Date();
    var birthDate = new Date($("#txtDateofBirth").val());
    var age = today.getFullYear() - birthDate.getFullYear();
    var m = today.getMonth() - birthDate.getMonth();
    if (m < 0 || (m === 0 && today.getDate() < birthDate.getDate())) {
        age--;
    }
    alert(age);
    if (age > 7) {
        $('#trMinor').hide();
    }
    else {
        $('#trMinor').show();
    }
});

I am not getting alert when value is entered in txtDateofBirth through ajax.



Solution 1:[1]

You have to trigger the event manually, it is only triggered automatically if the change was initiated by the user interface. Programmatically setting the value does not trigger the change event.

You can trigger an event with the .trigger() jQuery method:

$('#txtDateofBirth').val(saResponse[0]).trigger('change');
//or
$('#txtDateofBirth').val(saResponse[0]).change();

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 kapa