Open tab using anchor tag link?

admin

Administrator
Staff member
So I've got two tabs on a page, with anchor tags- I navigate to them from the homepage using the anchor on a header menu item.

I've gotten the actual tabs to work, using an active class and aria-expanded="true", but I can't get the appropriate tab to be active from the header.

i.e.:
1. Header menu->click on #tab2 link
2. Load page-> scroll to anchor tag-> tab2 is open (this is the part I have a problem with, tab 1 stays open unless I actually click on the tab2 button)

I've tried to change the active class involved, but I'm fairly new to javascript/jquery so I'm not sure I can actually do it; additionally, this is in worpress and the header menu doesn't have the field for an id of its own elements.

===============================

<h1>EDIT, CLARIFICATION: I have the tabs working, and I can go to them on the page from the header link, I just need the right tab to be open depending on the link I click from the header</h1>

I've also tried initiating a .click(#tab2) but the script won't run properly.

Code:
if(window.location.href.indexOf("tab2") &gt; -1) {
       document.getElementById('tab2').click();
    }

Edit:
Header HTML fot link:

Code:
&lt;ul class="uk-nav uk-nav-navbar"&gt;
        &lt;li class="uk-active"&gt;
            &lt;a title="" href="testsite.com/#tab1" &gt;tab 1&lt;/a&gt;&lt;/li&gt;
        &lt;li class="uk-active"&gt;&lt;a title="" href="testsite.com/#tab2" 
            class=""&gt;tab2&lt;/a&gt;
        &lt;/li&gt;
    &lt;/ul&gt;

Tab HTML

Code:
    &lt;li class="uk-width-medium-1-4 active" id="tab1" aria-expanded="false"&gt;
        &lt;a class="uk-button tab1"&gt;tab1&lt;/a&gt;
    &lt;/li&gt;
    &lt;li class="uk-width-medium-1-4 " id="tab2" aria-expanded="false"&gt;
        &lt;a class="uk-button tab2"&gt;tab2&lt;/a&gt;
    &lt;/li&gt;

Javascript:

Code:
var ai = document.getElementById(""); [I'm having trouble with this, cause I can't see a way to attach an id to a wordpress menu item]

var ch = document.getElementById("");

ai.onclick = function(){
    document.getElementById('tab1').click();
}

ch.onclick = function(){
    document.getElementById('tab2').click();
}