Cmd-click a link open in a new tab with JavaScript
We have a little jQuery plugin that makes entire table rows clickable by catching a click event on a table row and assigning window.location
to the href
of the anchor in that row – here's a simplified example:
... but this doesn't account for users who might want to hold down command
and click on a row to open it in a new tab. In order to do that, we check the e.metaKey
property that jQuery has so helpfully provided:
Voila! (Disclaimer: If you use something like this in the wild, make sure to do some other checks, like for multiple links in rows, the existence of checkboxes, that sort of thing – but this is the core to get you started.)
thanks to Pavel for correcting my initial implementation, in which I failed to realize metaKey is available on any jQuery event type)
Tweet