Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to open new tab with "ctrl+click" instead of redirect current tab, in jQuery?

Table contain digital values, each cell has each own href.

If I'm apply hrefs like this:

nTd.click(function(e){
   $(this).css("font-weight","bold");
   e.stopPropagation();
   location.href = 'http://google.com';
});

Each click on cell redirect window, I can't open new tab by "ctrl + click".

If I would add in TD something like '<a href="http://google.com"> 123123 </a>', then sorting through digital values would breaks, into lexicographical order.

like image 417
litigious.formalism Avatar asked Dec 10 '25 10:12

litigious.formalism


1 Answers

Make a check to see whether the CTRL key was down when the event occured:

nTd.click(function(e){
   $(this).css("font-weight","bold");
   e.stopPropagation();
    if(e.originalEvent.ctrlKey || e.originalEvent.metaKey){
        window.open("http://www.google.com", "_blank");
    } else {
        location.href = 'http://google.com';   
    }
});

JSFiddle

You won't see the page change in the fiddle, but you'll see the error it produces in the console.

like image 181
George Avatar answered Dec 12 '25 23:12

George