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:

 $(".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:

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

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

    Try instead of window.location.

  • answered 2018-10-11 19:32 Petar

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

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

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