Reputation: 116
<input type="text" name="a" id="a"/>
$("#a").focus();
var e = $.Event("keypress", { which: 65, keyCode: 65 });
$("#a").trigger(e);
Why is this not working for me? Tried various jquery versions too. js fiddle link: http://jsfiddle.net/hw48685m/3/
Upvotes: 4
Views: 4505
Reputation: 26
You can use jquery simulate extended library which is on Github and demo link also provided.
https://github.com/j-ulrich/jquery-simulate-ext By this, you can easily simulate keypress keyboard event to your input field with the letter by letter inputs.
Upvotes: 0
Reputation: 1694
Your code is working.... just add the following code and test the key-press trigger.
$('#a').keypress(function(e){
console.log('Yes KeyPress triggered. ' + e.which)
});
Upvotes: 1