simulate ctrl + click with javascript or jquery (to open a new tab without focus)

Solution 1


The Event object in jQuery has a parameter for ctrlKey, you could assign that as true, on click.

var e = jQuery.Event("click");
e.ctrlKey = true;

Reference: jquery trigger ctrl + click

Solution 2

In pure javascript, you can use the MouseEvent for that:

document.getElementById("todo").dispatchEvent(new MouseEvent("click", {ctrlKey: true}));

To programatically open a new tab you can do that:

const a = document.createElement("a");
a.setAttribute("href", "");
a.dispatchEvent(new MouseEvent("click", {ctrlKey: true}));

Solution 3

This is a non-jQuery version to simulate keyboard events. This works in both Chrome (WebKit based) and Firefox (Gecko based):

var keyboardEvent = document.createEvent("KeyboardEvent");
var initMethod = typeof keyboardEvent.initKeyboardEvent !== 'undefined' ? "initKeyboardEvent" : "initKeyEvent";

                   "keydown", // event type : keydown, keyup, keypress
                    true, // bubbles
                    true, // cancelable
                    window, // viewArg: should be window
                    false, // ctrlKeyArg
                    false, // altKeyArg
                    false, // shiftKeyArg
                    false, // metaKeyArg
                    40, // keyCodeArg : unsigned long the virtual key code, else 0
                    0 // charCodeArgs : unsigned long the Unicode character associated with the depressed key, else 0

Or using jQuery, you can simulate by jQuery's event object:

  type:  'keypress',
  which: character.charCodeAt(0)
Author by


Updated on June 18, 2022


  • Admin
    Admin about 2 months