Callum Whyte
Callum Whyte

Reputation: 2429

Loading tab by default in jQuery tab script

I have a jQuery tab script that gets content from a PHP file defined by the link and parses it to a div element. When a user selects a tab, it turns bold. However, currently when the script is loaded up, a tab is not selected by default. My question is how can I load up a certain tab and it's contents by default and show that the tab is selected with my current code?

This is my current jQuery code:

function load(url){
    $.ajax({
        url:url,
        success:function(message){
            $("#content").html(message);
        }
    });
}

$(document).ready(function(){
    $("[id]").click(function(){
        type=$(this).attr("id");
        url=""+type+".php";
        $("[id]").removeClass("selected");
        $("#"+type).addClass("selected");
        load(url);
        return false;
    });
});

This is my HTML code:

<ul> 
<li><a id="tab1" href="javascript:void(null);">Tab1</a></li> 
<li><a id="tab2" href="javascript:void(null);">Tab2</a></li> 
<li><a id="tab3" href="javascript:void(null);">Tab3</a></li> 
</ul>

Upvotes: 2

Views: 720

Answers (1)

fehays
fehays

Reputation: 3167

Trigger the click event of the tab you want to load: e.g. -

$(document).ready(function(){
    $("[id]").click(function(){
        type=$(this).attr("id");
        url=""+type+".php";
        $("[id]").removeClass("selected");
        $("#"+type).addClass("selected");
        load(url);
        return false;
    });
    //load default tab
    $("#tab1").click();
});

Upvotes: 1

Related Questions