Trigger Keypress with jQuery

Using trigger you are just triggering the event with a keycode but not assigning the value to the textbox. Try this :- http://jsfiddle.net/PbHD2/

String.fromCharCode

$("button").click(function() {
     $("input").focus();
    var e = jQuery.Event("keydown");
    e.which = 77; // # Some key code value
    $("input").val(String.fromCharCode(e.which));
    $("input").trigger(e);
});
$('input').keydown(function(e){
   console.log('Yes keydown triggered. ' + e.which)
});

Leave a Comment