Javascript entire div link WITH target="_blank"

I have set up a div with a button link. I have my javascript set up so if someone clicks anyway in the div, it will use the link text. Here's my current code:

 jQuery(function($){
 $(".entire-div-link").click(function() {
 window.location = $(this).find("a").attr("href"); 
 return false;
 });
 });

This works awesome, but the links still open in the current tab, and not in a new tab like I'd like. Is there an easy code correction I can use?

3 answers

  • answered 2018-10-11 19:07 Eugene Tsakh

    I'd suggest using a tag as a block instead of div:

    //css
    .block-link {
      display: block;
    }
    
    // html
    <a class="block-link" target="_blank">
      ...
    </a>
    

    https://jsfiddle.net/kjugdpnf/

  • answered 2018-10-11 19:24 Nimit Shah

    Try window.open instead of window.location.

    https://jsfiddle.net/nimittshah/t4wnsrkh/

  • answered 2018-10-11 19:32 Petar

    Just do this, it's way shorter with plain JS:

    document.querySelector(".entire-div-link").addEventListener("click", ()=>{
      window.open = "URL HERE"
    })
    

    As it was already mentioned, whether window.open will open a new tab OR a new window, depends solely on the user's browser configuration.