0

I'd like to be able to programmatically create a Ctrl-click or Cmd-click event, so that I could test my event-handling code, which looks like this:

// event is a jQuery event
if (event.metaKey || event.ctrlKey) {
    // do stuff
    openUrlInNewWindow(url);
} else {
    // do other stuff
    openUrl(this.clickedAd.url);
}

I'm calling the method with $(selector).trigger('click', event). I've tried creating the event like this:

jQuery.Event("click", {metaKey: true, keyCode: 91});

but jQuery transforms the event I pass in and seems to strip out the keyCode and metaKey attributes, so when I receive it in my event handler metaKey is always false.

What's the right way of creating a Ctrl-click event?

tro
  • 524
  • 4
  • 12

1 Answers1

1
var e = jQuery.Event( "click", { keyCode: 91, ctrlKey: true } );

$(".selector").trigger(e);

that work?

have a jsfiddle: http://jsfiddle.net/8n3u6/

Mikebert4
  • 156
  • 4
  • Thank you! I was actually triggering the event incorrectly. I was doing `$(selector).trigger('click', myEvent)`. :/ – tro Nov 18 '13 at 15:48