views:

40

answers:

1

the jquery code is like this:

    $(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("active"); //Remove any "active" class
        $(this).addClass("active"); //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;
    });

});

and the tabs:

    <ul class="tabs">
    <li><a href="#tab1">Gallery</a></li>
    <li><a href="#tab2">Submit</a></li>
</ul>

<div class="tab_container">
    <div id="tab1" class="tab_content">
        <!--Content-->
    </div>
    <div id="tab2" class="tab_content">
       <!--Content-->
    </div>
</div>

whenever the page loads the 1st tab is opened 1st. but if i want the 2nd tab to show, then how can i do that. like from inside a function im loading the page like: window.location="display.php"; when loaded i want the 2nd tab to open how to do that?? also from another function if i want the 1st tab to show how to do that?

thanks in advance..

+1  A: 

do it like this,

$(document).ready(function() {

    //When page loads...
    $(".tab_content").hide(); //Hide all content
    var index = $("ul.tabs li.active").show().index(); 
    $(".tab_content").eq(index).show();

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

        $("ul.tabs li").removeClass("active"); //Remove any "active" class
        $(this).addClass("active"); //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;
    });

});

then on the tabs (li) on the html, set an active like,

<ul class="tabs">
    <li class="active"><a href="#tab1">Gallery</a></li>
    <li><a href="#tab2">Submit</a></li>
</ul>

<div class="tab_container">
    <div id="tab1" class="tab_content">
        <!--Content-->
    </div>
    <div id="tab2" class="tab_content">
       <!--Content-->
    </div>
</div>
Reigel
ya i did.. but now when the page is reloaded tab2 is being opened everytime..
champ
I have a typo on setting the class on `li` above. when you want `tab2` set the `class="active"` on tab2 `li`. Not by javascript that is. for example, in `display.php`, `<li class="active"><a href="#tab1">Gallery</a></li>` and on another page, `<li class="active"><a href="#tab2">Submit</a></li>`
Reigel
the same problem remailns.. tab1 is displayed everytime, isnt there any way while im leaving the page the active tab will remain active after reload??
champ
Yup! the concept of setting the class active on different conditions worked.. thanxx a lot!!
champ