I'm trying to simulate how the cursor moves in an input text field on left arrow key press in one of my tests, but it seems that the event doesn't work (maybe for security reasons?). Does anyone know a workaround?
Here a jsFiddle with an example: http://jsfiddle.net/UYW6M/.
$(function () {
$('#check').click(function(e) {
e.preventDefault();
$('input').focus();
console.log($('input')[0].selectionStart);
});
$('#move').click(function(e) {
e.preventDefault();
var press = jQuery.Event("keypress");
press.ctrlKey = false;
press.which = 37;
$('input').focus();
setTimeout(function(){
$('input').trigger(press);
console.log('event triggered!');
}, 1000);
});
});
Key 37 is left arrow. Also doesn't work with other code (like 65 for inserting an "a").
I'm using jQuery here for simplicity, but my app is actually in Angular + Karma, in case someone knows a solution for that combo.