Why wouldn’t onChange function of an input be activated when the value of the input is changed by javascript? For example why when clicking on the button alert wouldn’t happen?
<html>
<body>
<input type="text" id="myInput" onchange="alert(1)" />
<button onclick = "document.getElementById('myInput').value = 'ads'">click</button>
</body>
</html>
How can I make the onchange function work then?
edit:
I now understand event won’t be triggered automatically, how can I call it(jquery too) ?
Scripted value changes don’t trigger an event. You have to manually trigger an event.
jQuery:
$('#myInput').trigger('change');Add the following code to the
onchangeevent:So, combined with your code (you should actually separate the JS code from the HTML):