Search code examples
javascripthtmlcssonclickhref

How to select and attribute in span tag when call window.open function


I have a span tag and I want to execute onclick function that call two window.open :

<span data-href="https://clkde.tradedoubler.com/click?p=300883&a=3040124&g=24628776" class="BUTTON_NZV" style="border-radius: 7px;
    color: #FFFFFF;
    font-family: Arial;
    font-size: 31px;
    font-weight: 600;
    padding: 15px;
    background-color: #1BC325;
    text-decoration: none;
    display: inline-block;
    cursor: pointer;
    text-align: center;

" onclick="window.open('#','_blank');window.open(this.data-href,'_self');">OK</span>

The behavior I want is to open the actual page in a new tab and focus on it and open the data-href link in the previous page without focus on it! Can you help me to do that?


Solution

  • Use the getAttribute function of this.

    <span data-href="https://clkde.tradedoubler.com/click?p=300883&a=3040124&g=24628776" class="BUTTON_NZV" style="border-radius: 7px;
        color: #FFFFFF;
        font-family: Arial;
        font-size: 31px;
        font-weight: 600;
        padding: 15px;
        background-color: #1BC325;
        text-decoration: none;
        display: inline-block;
        cursor: pointer;
        text-align: center;
    
    " onclick="window.open('#','_blank');window.open(this.getAttribute('data-href'),'_self');">OK</span>