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.

1
  • IIRC, the "change" event won't fire until the input loses focus. Perhaps your "custom js keyboard" should emit events? Commented Apr 24, 2018 at 21:01

1 Answer 1

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">

Sign up to request clarification or add additional context in comments.

1 Comment

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!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.