Creating a Next button for switching through jQuery tabs
Asked Answered
S

3

19

How can I create a button that will scroll to the next jQuery tab. I want to have a next button within the tabs that will scroll to the next tab, sort of like a step-by-step tutorial.

How can this be done? My code so far is below.

HTML

<div id="tabs">
    <ul>
        <li><a href="#fragment-1"><span>One</span></a></li>
        <li><a href="#fragment-2"><span>Two</span></a></li>
        <li><a href="#fragment-3"><span>Three</span></a></li>
    </ul>
    <div id="fragment-1">
        <p>First tab is active by default:</p> <a href="nexttab">Next Tab</a>
    </div>
    <div id="fragment-2">Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh</div>
    <div id="fragment-3">Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh</div>
</div>

JS

$(document).ready(function () {
    $("#tabs").tabs();
});
Segment answered 15/6, 2010 at 11:23 Comment(0)
P
24

You can use the selected option to move around, like this:

$(".nexttab").click(function() {
    var selected = $("#tabs").tabs("option", "selected");
    $("#tabs").tabs("option", "selected", selected + 1);
});

Just change your anchor to match, like this:

<a class="nexttab" href="#">Next Tab</a>

You can view a demo here


Alternatively, make each "Next Tab" link point to a specific tab and use the select method, like this:

<a class="nexttab" href="#fragment-2">Next Tab</a>

Then you can use a bit shorter jQuery, and move to any tab you want:

$(".nexttab").click(function() {
    $("#tabs").tabs("select", this.hash);
});

Here's a demo of this approach

Peppery answered 15/6, 2010 at 11:31 Comment(3)
this.hash is the best answer I have seen. +1Malapropos
please make a circular navigation!Dependent
Even though this is an old answer, but can somebody explain me what does, and why this.hash works?Neolamarckism
P
13

I found that with UI 1.10.0 this solution no longer works, as "selected" was deprecated. The following will work in both 1.10 and earlier versions-

$("#tabs").tabs();
$(".nexttab").click(function() {
    var active = $( "#tabs" ).tabs( "option", "active" );
    $( "#tabs" ).tabs( "option", "active", active + 1 );

});
Phosphorescent answered 7/2, 2013 at 1:55 Comment(1)
This is the correct answer @Phosphorescent ! Do not use selected anymore - instead use the active optionInterrogation
T
0

Based on Nick Craver's answer, here's how I produced the same functionality using next-buttons that look like this in the HTML at the bottom within each tab div:

<button class="btnNext" style="float:right">Next</button>

Based on Nick's answer I created two functions:

function moveToNextTab()
{
        var selected = $("#tabs").tabs("option", "selected");
        $("#tabs").tabs("option", "selected", selected + 1);
}
function EnableButtons(className)
{
    //Enable Next buttons
    var aryButton = document.getElementsByTagName("button");
    for(var i = 0; i < aryButton.length; i++)
    {
        var e = aryButton[i];
        if(e.className == className)
        {
            e.onclick = function()
            {
                moveToNextTab();
                return false;
            };
        }
    }   
}

Since each button belongs to the "btnNext" class, after the page loads, I call:

onload = EnableButtons("btnNext");

and this enables each button's ability to move to the next tab.

Tumble answered 15/2, 2013 at 19:49 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.