5
$(document).keypress(function(event) {
    // +
    if (event.which == 43) {
        // ...
    }
}

HTML

<input type="button" value="+" name="plus">

How can I trigger the keypress method with + when clicking the button?

$('input[name="plus"]').click(function(){
    // ??? How to go further ???
})
Brian Tompsett - 汤莱恩
  • 5,438
  • 68
  • 55
  • 126
powtac
  • 39,317
  • 26
  • 112
  • 166

2 Answers2

8

Here you go

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

Src: Definitive way to trigger keypress events with jQuery

Community
  • 1
  • 1
Eddie
  • 12,535
  • 3
  • 24
  • 31
1
$(document).on('keypress',function(e) {
    if (e.keyCode == 43 || e.which == 43) {
        var identifier = e.target.id;
        console.log(e.target.id);
        $('#' + identifier).click();
    }
});
djrconcepts
  • 625
  • 5
  • 6
  • I need it the other way round: When I click the button a keypress should be virtually triggered. – powtac Apr 24 '12 at 10:00