rjx44
rjx44

Reputation: 389

trigger a keypress on click event

This code does not work for me:

var e = jQuery.Event("keypress");

e.which = 43; // # Some key code value

$(document).trigger(e);

Is there anyway to trigger it virtually?

$("input#genres").live('click', function(){

 var value = $(this).val();

 if($(this).is(':checked')) { 

 $("input.maininput").val(value);
 var e = jQuery.Event("keypress");

    e.which = 13; // # Some key code value

    $("input.maininput").trigger(e);

}

});

This is my whole code but still no luck.

Upvotes: 1

Views: 2969

Answers (2)

Michal Klouda
Michal Klouda

Reputation: 14521

Your code is working, you just need to add some handler..

var e = jQuery.Event("keypress");
e.which = 43; // # Some key code value

$(document).on('keypress', function(e) {
   alert(e.which);
});

$(document).trigger(e);​

Upvotes: 1

rahul
rahul

Reputation: 7663

you can do it like this

var e = jQuery.Event("keydown");
e.which = 8; // some value (backspace = 8)
$(document).on('keydown', function(e) {
   alert(e.which);
});

$(document).trigger(e);​

Upvotes: 0

Related Questions