There is a lot of contents on this in stack overflow but none seems to work for my case. I have an input text field and I want to simulate keypress event to fill the text field.
Reason: I am automating a lot of data entry task on a web interface which provides no API. Changing the input field using .value
does not trigger the JS side (angular) of the interface. That is why I want to simulate keypress event.
First I tried this:
var inp = document.getElementById('rule-type');
inp.dispatchEvent(new KeyboardEvent('keypress',{'key':'a'}));
Then I learned in Chrome the key
and code
stays as 0 and does not change in KeyBoardEvent
.
So I created seperate event ev = new KeyboardEvent('keypress',{'key':'a', 'code': 'KeyA'})
And then I dispatched again, the return statement is true
but it does not change the input field.
The solution needs to be in pure javascript not jQuery.
If you need keyup events too, it is also possible to simulate keyup events by changing "keydown" to "keyup" in the code snippet. This also sends the event to the entire webpage, hence the document .
To record a keypress event in JavaScript, use the code below: // Add event listener on keypress document. addEventListener('keypress', (event) => { var name = event. key; var code = event.
Deprecated: This feature is no longer recommended. Though some browsers might still support it, it may have already been removed from the relevant web standards, may be in the process of being dropped, or may only be kept for compatibility purposes.
keydown: This event is triggered when a key is pressed down. keypress: This event is triggered when a key is pressed. This event fails to recognise keys such as tab, shift, ctrl, backspace etc. keyup: This event is triggered when a key is released.
You will not be able to fire an event that will cause text to populate an input. See this snippet from MDN:
Note: manually firing an event does not generate the default action associated with that event. For example, manually firing a key event does not cause that letter to appear in a focused text input. In the case of UI events, this is important for security reasons, as it prevents scripts from simulating user actions that interact with the browser itself.
Thanks @gforce301
Your best bet may be to set the value and then dispatch an event.
IMHO you're going about this all wrong. Angular is not "listening" to keypress
or the like. It is listening to change
and input
events. See the example below. It does (admittedly it is simple) what you need.
var iButton = document.getElementById('inputButton');
iButton.addEventListener('click', simulateInput);
var cButton = document.getElementById('changeButton');
cButton.addEventListener('click', simulateChange);
function simulateInput() {
var inp = document.getElementById('name');
var ev = new Event('input');
inp.value =inp.value + 'a';
inp.dispatchEvent(ev);
}
function simulateChange() {
var inp = document.getElementById('name');
var ev = new Event('change');
inp.value = 'changed';
inp.dispatchEvent(ev);
}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="">
<p>Input something in the input box:</p>
<p>Name : <input id="name" type="text" ng-model="name" placeholder="Enter name here"></p>
<h1>Hello {{name}}</h1>
</div>
<button id="inputButton">I simulate the "input" event.</button>
<button id="changeButton">I simulate the "change" event.</button>
To automate filling <input>
and <textarea>
values and automatically dispatch all needed events you need:
insertText
commandlet inputElement = document.getElementsById('rule-type');
inputElement.focus();
document.execCommand('insertText', false, 'input value');
With this method input\textarea value modification should be captured by all major frameworks including Angular an Vuejs. This modification will be processed by frameworks the same way as if user pressed "Paste" option in browser main menu.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With