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.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…