-4

How to trigger an event when a value is passed by javascript to an input box

<!DOCTYPE html>
<html>
<body>

<p>Write something in the text field to trigger a function.</p>

<input type="text" id="myInput" oninput="myFunction()">

<p id="demo"></p>

<script>

document.getElementById("myInput").value = 100;

function myFunction() {
    var x = document.getElementById("myInput").value;
    document.getElementById("demo").innerHTML = "You wrote: " + x;
}
</script>

</body>
</html>

When the value 100 auto generated, "You Wrote 100" should be displayed

zaster
  • 341
  • 3
  • 5
  • 16
  • 1
    Note that I found that duplicate by searching on your title; it was the second result. [Please do some research before asking on Stack Overflow](http://idownvotedbecau.se/noresearch/). – Heretic Monkey Apr 25 '18 at 00:23
  • @MikeMcCaughan Noted. I will make sure to research more in the future. Thanks for the guide. – zaster Apr 25 '18 at 00:36

1 Answers1

0

I don't really know you problem, it's working fine here. JS example:

document.getElementById("myInput").value = 100;

function myFunction() {
    var x = document.getElementById("myInput").value;
    document.getElementById("demo").innerHTML = "You wrote: " + x;
}
<p>Write something in the text field to trigger a function.</p>

<input type="text" id="myInput" onkeyup="myFunction()">

<p id="demo">You wrote: 100</p>

jQuery example:

$('#myInput').val(100);

$('#myInput').keyup(function(){
  $('#demo').html('You wrote: ' + $('#myInput').val());
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<p>Write something in the text field to trigger a function.</p>

<input type="text" id="myInput">

<p id="demo">You wrote: 100</p>

I would recommend you to use onkeyup because it's more dynamic than onchange.

Pang
  • 9,564
  • 146
  • 81
  • 122
Bambou
  • 1,005
  • 10
  • 24