0

I have an input box with id #tDate. I use jquery to update the value in the field but an on change event for the field is not working. The on change events works fine if I manually update the value in the field.

Below is my code

$("#tDate").val("2018/04/30");

//Then I have this on change event
$("#tDate").on("change", function (e, args){
    alert('value changed');
});

I would like the on change events to work even if the input box value is changed with JQuery.

Thank you.

iamPacMan
  • 35
  • 6

2 Answers2

2

You'll have to trigger change event after setting value like

$("#tDate").val("2018/04/30");
$('#tDate').trigger('change');

Or in one line

$("#tDate").val("2018/04/30").trigger('change');

This should trigger your change event handler.

Muhammad Usman
  • 10,039
  • 22
  • 39
0

JS:

$("#tDate").val("2018-04-30");

$('#some-field').on("change", function (e, args){
  $("#tDate").val("2018-05-01");
  alert('value changed');
});

HTML

<input type="text" id="some-field" />
<input type="date" id="tDate" />

https://codepen.io/mdlanglais/pen/XqMzQQ

Formatting the date with slashes isn't working for me, dashes do for what that's worth. If it's not a problem for you then it's not a problem! In any case, the date field is updating via jQuery with onchange.

mdlanglais
  • 351
  • 2
  • 9