GLP
GLP

Reputation: 3685

how to set default button in different tabs?

I have a content page that have 4 different tabs. I use following jQuery to control the tabs. I need to set the default button for each tab, so that when the user clicks Enter, the event of the default button will be fired.

The keypress should do the work. But somehow, when I click Enter, the page is reloaded, not being submitted.

How can I change my code to have the page determine which button is the default button, depending on what user control or tab is active?

$(document).ready(function() {
    var selected_tab = document.getElementById("<%=hfSelectedTab.ClientID %>");
    //When page loads...
    $(".tab_content").hide(); //Hide all content
    if (selected_tab.value == 2) {
        $("ul.tabs li:nth-child(2)").addClass("active").show();
        $(".tab_content:nth-child(2)").show();
    }
    else if (selected_tab.value == 3) {
        $("ul.tabs li:nth-child(3)").addClass("active").show();
        $(".tab_content:nth-child(3)").show();
    }
    else if (selected_tab.value == 4) {
        $("ul.tabs li:nth-child(4)").addClass("active").show();
        $(".tab_content:nth-child(4)").show();
    }
    else {
        $("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

        if (activeTab == "#tab1")
            selected_tab.value = 1;
        else if (activeTab == "#tab2")
            selected_tab.value = 2;
        else if (activeTab == "#attachmentcontent")
            selected_tab.value = 3;
        else if (activeTab == "#kb") {
            selected_tab.value = 4;
            document.getElementById("<%=txtRootCause.ClientID %>").focus();
        }

        return false;
    });

    $("form input").keypress(function(e) {
        if ((e.which && e.which == 13) || (e.keyCode && e.keyCode == 13)) {
            $('button[type=submit] .default').click();
            return true;
        }
    });
});

Upvotes: 2

Views: 738

Answers (1)

GLP
GLP

Reputation: 3685

My solution is to wrap each tab (defined by div) in an asp:Panel and set the Panel DefaultButton property.

Upvotes: 3

Related Questions