#TIL : Trigger event after setting val in jQuery

After setting value of an input via val method, we should call the change chaining method to trigger the onChange event of element.

1
2
3
4
5
6
7
$('#selectCity').change(function() {
console.log($(this).val());
});

$('#selectCity').val('HaNoi'); // No trigger

$('#selectCity').val('HoChiMinh').change(); // Fire trigger

Comments