0

I want to recognize a function event with "on" and then run console.log but input, keyup, keydown or change doesnt call this change.

I have the write function.

function write(){
  $("input").val("1");
}

And then this query for checking the event:

$("input").on("change",function(){
  console.log('input changed!');
});

write();//it doesnt get the change function

Custom HTML:

<input type="text">

The reason of this is that i want to enter values with a custom js keyboard (that's why i cant use keyup or keydown) but i cant get the event of .val() from jquery.

Miguel Ramirez
  • 83
  • 2
  • 14

1 Answers1

1

You have to trigger the event after you set .val - jQuery doesn't trigger events automatically when you change it in code.

function write(){
  $("input").val("1").trigger("change");
}

$("input").on("change",function(){
  console.log('input changed!');
});

write();//it doesnt get the change function
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text">
dave
  • 62,300
  • 5
  • 72
  • 93
  • This was the answer i was looking for, i didnt know it was a duplicate because i didnt know how exactly make the question but thanks! – Miguel Ramirez Apr 24 '18 at 21:09