Dr Casper Black
Dr Casper Black

Reputation: 7478

jquery how to show specific tab with link from another page

how to show specific tab with link from another page

<a href="index.php?page=home#tab2">Home</a>

this is the JS code:

$(document).ready(function() {

    //When page loads...
    $(".tab_content").hide(); //Hide all content
    //$("ul.tabs li:first").addClass("active").show(); //Activate first tab
    $(".tab_content:first").show(); //Show first tab content

    //On Click Event
    $("ul.tabs li").click(function() {

        $("ul.tabs li").removeClass("selected"); //Remove any "active" class
        $(this).addClass("selected"); //Add "active" class to selected tab
        $(".tab_content").hide(); //Hide all tab content

        var activeTab = $(this).find("a").attr("href"); //Find the href attribute value to identify the active tab + content
        $(activeTab).fadeIn(); //Fade in the active ID content
        return false;
    });


});

Upvotes: 0

Views: 6290

Answers (2)

Jethro Larson
Jethro Larson

Reputation: 2345

Something like this maybe?

var openTab = $(location.hash).filter(".tab_content");

if(openTab.length){
  $("a[href='"+location.hash+"']").click();
}

Upvotes: 2

Matt
Matt

Reputation: 75327

If you're looking at using the hash in a URL to pre-select a tab when the page loads, simply use window.location.hash to store an identifier of the current selected tab (element ID?), then read window.location.hash when the document ready event fires, and react to any element ID in there.

Upvotes: 1

Related Questions