2

I have a repeater control. Inside each element/record, I have a tabbed interface with 2 tabs. I am implementing the tab-functionality with Jquery. But each time I click on any tab on the page, only the first element/record's tabs are switching. Please let me know how to achieve this.

<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>



$(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;
    });

});
Praneeth
  • 31
  • 3

1 Answers1

1

Since you are using ids to tab content divs in the repeater. All repeater elements will have the sane ids so your logic will not. Try this

$(document).ready(function() {

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

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

        $(this).siblings().removeClass("active"); //Remove any "active" class
        $(this).addClass("active"); //Add "active" class to selected tab

        var $tabContents = $(this).parent().next().children();
        $tabContents.hide(); //Hide all tab content

       // var activeTab = $(this).find("a").attr("href"); //Find the href attribute      value to identify the active tab + content

        //Use the index() method to get the tab to show 
        $tabContents.eq($(this).index()).fadeIn();

        return false;
    });

});

Also set the href property of anchor to javascript:void(0) otherwise the page will scroll when you click on the tab.

ShankarSangoli
  • 69,612
  • 13
  • 93
  • 124