4

How can I activate a "left" or "right" keyboard arrow push on click of a div.

So for example

$('.item').click(function(){
    keyCode(37);
});

(I know that 37 is left)

0

3 Answers 3

7

You would go like

$('.item').click(function(){
    $( document.body ).trigger({
        type: 'keypress',
        which: 37,
        keyCode: 37
    });
});

You can of course replace document.body with any other node that has a keypress or keydown event bound to it.

Reference: .trigger()

Sign up to request clarification or add additional context in comments.

Comments

2

From Definitive way to trigger keypress events with jQuery:

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

Comments

1

not sure, but can you try this.

$('.item').click(function(){ 
      $('body').trigger("keypress", [{ 
        preventDefault:function(){}, 
        keyCode:13 
     }]); 
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.