Input event not working if value is changed with jQuery val() or JavaScript
<p>If I change the value of an input field programmatically, the <code>input</code> and <code>change</code> events are not firing. For example, I have this scenario:</p>

<p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
<div class="snippet-code">
<pre class="snippet-code-js lang-js prettyprint-override"><code>var $input = $('#myinput');

$input.on('input', function() {
  // Do this when value changes
  alert($input.val());
});

$('#change').click(function() {
  // Change the value
  $input.val($input.val() + 'x');
});</code></pre>
<pre class="snippet-code-html lang-html prettyprint-override"><code>&lt;input id="myinput" type="text" /&gt;
&lt;button id="change"&gt;Change value&lt;/button&gt;

&lt;script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"&gt;&lt;/script&gt;</code></pre>
</div>
</div>
</p>

<p><strong>The problem:</strong> The event is triggered when I type in the textfield, but not when I press the button. Is there a way to achieve this with some kind of event or otherwise without having to do it manually? </p>

<p><strong>What I don't want to do:</strong> I could go through all my code to add a <code>trigger</code> or function call everywhere manually, but that's not what I'm looking for.</p>

<p><strong>Why:</strong> The main reason I would like to do this automatically is that I have a lot of input fields and a lot of different places where I change these inputs programmatically. It would save me a lot of time if there was a way to fire the event <em>automatically</em> when any input is changed anywhere in my code.</p>