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,753
  • 72
  • 57
  • 129
powtac
  • 40,542
  • 28
  • 115
  • 170

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,898
  • 3
  • 25
  • 32
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
  • 635
  • 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