Search code examples
jquerytriggerskeypresskeyupsimulate

How to simulate a keypress or Keyup when a link is clicked with jQuery


I am working with DataTable and TableTools plugins with jQuery.

I am trying to simulate a keyPress (or keyUp), when someone clicks a link in the page.

html code :

<a href='#' id='return'>رجوع</a>

jQuery code :

$('#return').click(function() {
    e.preventDefault();
    $(this).trigger(// code here // );
});

I can't find the code to put there.


Solution

  • Try this

    $(this).trigger('keyup');
    

    DEMO

    Update

    Try this :

    $('#return').click(function(evt) {
        e.preventDefault();
        var evt = jQuery.Event('keyup');
        evt.keyCode = 27;
        evt.which = 27;
        $(this).trigger(evt);
        alert(evt.keyCode);
    });
    

    DEMO