Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Trigger keypress on button click

$(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 ???
})
like image 327
powtac Avatar asked Jul 01 '11 13:07

powtac


2 Answers

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

like image 193
Eddie Avatar answered Sep 18 '22 23:09

Eddie


$(document).on('keypress',function(e) {
    if (e.keyCode == 43 || e.which == 43) {
        var identifier = e.target.id;
        console.log(e.target.id);
        $('#' + identifier).click();
    }
});
like image 37
djrconcepts Avatar answered Sep 20 '22 23:09

djrconcepts