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?