javascriptjqueryhtmlsortinglexicographic

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.


Solution

  • 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.